You can use below method to update product attribute value.
$product = $this->productRepository->getById($productId);
$product->setData($attributeCode, $attributeValue2);
$this->productRepository->save($product);
Also, if you want $objectManager
method to create a script. You can do it by following below code snippet.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
// Set the state. You can also set "adminhtml"
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
// Get the first product of a collection
$products = $obj->get('Magento\Catalog\Model\ResourceModel\Product\Collection');
foreach ($products as $p) {
$p->setData($attributeCode, $attributeValue);
$p->getResource()->saveAttribute($p, $attributeCode);
}
Hope it helps!!!