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);
}
}
}
Answer by UsmanAbbasi · Jun 12, 2016 at 10:15 AM
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: https://docs.unity3d.com/ScriptReference/Renderer-material.html https://docs.unity3d.com/ScriptReference/Renderer-sharedMaterials.html
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. $$anonymous$$y error was that I was changing the color of the parent and not the cylinder thingy when I was working with the fbx.
Your answer
Follow this Question
Related Questions
Is there a way to control when lighting is generated? 0 Answers
generating hallways using prefabs based on difficulty 1 Answer
Prefab block based level generation | Generating a level from prefab blocks 0 Answers
My public variables are lost when I convert my game object into a prefab 1 Answer
I need help adding an initial upwards velocity to some cloned prefabs. 0 Answers