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)
Now , here is my hierarchy
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.