Different color for each instance of a prefabs

So I’m currently trying to generate a world using a prefab with only a MeshRenderer on it. My goal is to generate a world and having each instances having a different color. I heard that you need to create a new material every time when u do so but it is not working right now, Any idea why ?

public class HexGeneration : MonoBehaviour {

public GameObject platformMesh;
public int height = 5;
public int lenght = 5;

// Use this for initialization
void Start () {
    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < lenght; j++)
        {
            GameObject generatedPlatform = Instantiate(platformMesh, new Vector3(0,0,0), Quaternion.identity) as GameObject;

            Transform platformTransform = generatedPlatform.GetComponent<Transform>();
            ChangePosition(platformTransform, i, j);

            MeshRenderer platformRenderer = generatedPlatform.GetComponent<MeshRenderer>();
            ChangeMaterial(platformRenderer);
            
        }
    }
}

// Update is called once per frame
void Update () {

}

void ChangeMaterial(MeshRenderer platformRenderer)
{
    Material material = new Material(Shader.Find("Standard"));
    Color color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);

    material.color = color;

    platformRenderer.material = material;
}

void ChangePosition(Transform platformTransform, float height, float lenght)
{
    if (height % 2 == 0)
        platformTransform.Translate(lenght * 8.66f, 0, -height * 7.5f);
    else
    {
        if (height == 1) 
            platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -7.5f);
        else
            platformTransform.Translate((lenght * 8.66f) + 4.33f, 0, -height * 7.5f);

    }
}

}

The enemies are still there. I can't destroy them.

1 Answer

1

You can change color like this:

 void ChangeMaterial(MeshRenderer platformRenderer)
 {
     Color color = new Color(Random.Range(0, 1), Random.Range(0, 1), Random.Range(0, 1), 1);
     platformRenderer.material.color = color;
 }

Accessing material property using “renderer.material” automatically creates new material for that object so you don’t have to create it. You should know the difference between “material” and “sharedMaterial” property.
This two links will help you understand this concept:

And by “it is not working”, do you mean that color is not changing or are you having any errors?

Tip: If color is not changing then try to assign color like this just to make sure that issue is with color assigning and not somewhere else:

 platformRenderer.material.color = Color.red;

Yeah it's good I have it working now, I understood what my problem was. You see, I was working with .FBX and when I was making my prefab I didn't realise that it came with a Light, A Camera, and a Cylinder thingy. So what I did was I created my mesh again, exported it in OBJ, and when I imported the mesh I still put the mesh as default but as a child of something. So I took default and made it my prefab. My error was that I was changing the color of the parent and not the cylinder thingy when I was working with the fbx.

Ok... Let me try to break it down: On EACH Enemy Start(): Add Self to Master Enemy List. Master Enemy List <-- Holds all the enemies. Master Defeated List (Don't Destroy On Load): On Scene A: (On Scene Load): Parse through Master Defeated List... Loop through all units in Master Enemy List, if that enemy is also on the defeated list, destroy/disable it. On Scene B: On defeat, add to Master Defeated List. Switch back to Scene A...which will loop through and delete any instances of the defeated.