Setting Textures on multiple objects via script

All the search results I got were from 2012 and earlier and most of them are deprecated.
I am trying to set a texture to 200 game objects using a simple class however I am having a hard time accessing the appropriate component.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SM_Bld_Texture
{
    [MenuItem("Tools/Assign BLD Texture")]
    public static void AssignTileMaterial()
    {
        GameObject[] tiles = GameObject.FindGameObjectsWithTag("SM_Bld");

        Texture texture = Resources.Load<Texture>("T_PolygonTown_01_A");

        foreach (GameObject t in tiles)
        {
            t.GetComponent<MeshRenderer>().material.SetTexture("T_PolygonTown_01_A", texture);

        }
    }
}

I am a bit at a loss… am I even accessing the right component?

If all your tiles share same material, it should be enought to

t.GetComponent<MeshRenderer>().sharedMaterial.SetTexture("T_PolygonTown_01_A", texture);

For any single tile. Note that .sharedMaterial i’ve used instead of .matrerial in your code.

Perfect, thank you very much :slight_smile: