Help with script , Change tag depending on material attached

Hi all , i have this problem that is making me itch my head. I have a grid of tiles , and instead of changing their tags manually, i want it to be possible for me to add a script and have the the tiles change their own tag based on what material colour i attach to them.

The Materials im using look like this , Each having a default “UI” shader (The reason i used the Default UI shader is because its the only one that brings out the colour without needing a light source. If this is wrong or a better way , please correct me)

110220-colours.png

Now , here is my hierarchy
110221-grid.png

Lastly , this is my Code , Which is attached to “TileGrid”

    public Material Red;
    public Material Yellow;
    public Material Blue;

    void Start()
    {
        Transform TileGrid = transform;                             
        int GridChildCount = TileGrid.childCount;                   

        for (int i = 0; i < GridChildCount; ++i)                    
        {
            Transform GridRow = TileGrid.GetChild(i);              
            int setChildCount = GridRow.childCount;                 

            for (int j = 0; j < setChildCount; ++j)                   
            {
                SetTagAccordingToMaterial(GridRow.GetChild(j));    
            }
        }
    }

    private void SetTagAccordingToMaterial(Transform Tile)
    {
        Renderer TileRenderer = Tile.GetComponent<Renderer>();

        if (TileRenderer.material == Red)
        {
            tag = "Red";
        }
        else if (TileRenderer.material == Yellow)
        {
            tag = "Yellow";
        }

        else if (TileRenderer.material == Blue)
        {
            tag = "Blue";
        }
    }
}

So to give an idea of what the code above is meant to do, remembering that this script is attached to “TileGrid” , is when I click run first get the material of the Children’s Children (so basically the “Tile”). Than to change the Tag of “Tile” depending on the attached material.

Also , I drag all 3 materials/colours into the “public Material Red/Yellow/Blue” in Unity.

So what’s happening ?

Basically Nothing , Whenever i run , the Tags do not change.

I’m wondering if maybe the material i am using isn’t recognized by the GetCompontent ?

Any Help would be gladly appreciate it , Im also not 100% sure if my Loops to get the rendered in the first place work properly as i haven’t been able to test it since nothing is working.

There are two problems with your SetTagAccordingToMaterial method:

  1. The condition :

    TileRenderer.material == Red
    

does not work. It works when you compare names.

TileRenderer.material.name == Red.name + " (Instance)"
  1. In the statement

    tag = “Red”
    tag refers to your TileGrid’s tag, but what you want is each Tile’s tag. So change it to:

Tile.tag = "Red"

And the same for the other two colors.
Of course, don’t forget to add those three tags to Project Settings->Tags and Layers

    private void SetTagAccordingToMaterial(Transform Tile)
    {
    	Renderer TileRenderer = Tile.GetComponent<Renderer>();
    
    	if (TileRenderer.material.name == Red.name + " (Instance)")
    	{
    		Tile.tag = "Red";
    	}
    	else if (TileRenderer.material.name == Yellow.name + " (Instance)")
    	{
    		Tile.tag = "Yellow";
    	}
    	else if (TileRenderer.material.name == Blue.name + " (Instance)")
    	{
    		Tile.tag = "Blue";
    	}
    }