Search Unity

Change MeshRenderer color of prefab instance after creation

Discussion in 'Scripting' started by pr0fess0r, May 24, 2020.

  1. pr0fess0r

    pr0fess0r

    Joined:
    May 24, 2020
    Posts:
    1
    Hi Guys,

    I'm new to Unity and I've seen a lot of different answers to this question but I can't get any of them to work for me.
    I've got a prefab script that creates a quad (from the Unity docs), and I'm using a script to instantiate a bunch of these, and I want each one to have a different color (later they'll have other properties as well).

    This is my QuadCreator prefab:

    Code (CSharp):
    1. public class QuadCreator : MonoBehaviour
    2. {
    3.     public float width = 1;
    4.     public float depth = 1;
    5.     public Color color = Color.green;
    6.     public GameObject Quad;
    7.     public MeshRenderer meshRenderer;
    8.  
    9.     public void Start()
    10.     {
    11.         meshRenderer = gameObject.AddComponent<MeshRenderer>();
    12.         meshRenderer.material = new Material(Shader.Find("Standard"));
    13.         meshRenderer.material.color = color;
    14.         MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
    15.  
    16.         Mesh mesh = new Mesh();
    17.  
    18.         Vector3[] vertices = new Vector3[4]
    19.         {
    20.             new Vector3(0, 0, 0),
    21.             new Vector3(width, 0, 0),
    22.             new Vector3(0, 0, depth),
    23.             new Vector3(width, 0, depth)
    24.         };
    25.         mesh.vertices = vertices;
    26.  
    27.         int[] tris = new int[6]
    28.         {
    29.             0, 2, 1,
    30.             2, 3, 1
    31.         };
    32.         mesh.triangles = tris;
    33.  
    34.         Vector3[] normals = new Vector3[4]
    35.         {
    36.             -Vector3.forward,
    37.             -Vector3.forward,
    38.             -Vector3.forward,
    39.             -Vector3.forward
    40.         };
    41.         mesh.normals = normals;
    42.  
    43.         Vector2[] uv = new Vector2[4]
    44.         {
    45.             new Vector2(0, 0),
    46.             new Vector2(1, 0),
    47.             new Vector2(0, 1),
    48.             new Vector2(1, 1)
    49.         };
    50.         mesh.uv = uv;
    51.         meshFilter.mesh = mesh;
    52.     }
    53. }
    I don't have an actual quad in my scene, the prefab is just the script.
    I have a script called Ground that instantiates a bunch of quads like this:

    Code (CSharp):
    1. public GameObject Quad;
    2.     // Start is called before the first frame update
    3.     void Start()
    4.     {
    5.         for(int i = 1; i <= 5; i++)
    6.         {
    7.             var obj = GameObject.Instantiate(Quad, new Vector3(i*5.0f, -2.5f, -2.5f), Quaternion.identity) as GameObject;
    8.             Color col = Random.Range(0, 2) == 1 ? Color.black : Color.white;
    9.             var objRenderer = obj.GetComponent<MeshRenderer>();
    10.             objRenderer.material.color = col;
    11.         }
    12.     }
    At runtime this creates the 5 quads fine, they're green, but trying to change the color of each instance gives this error:

    MissingComponentException: There is no 'MeshRenderer' attached to the "Quad(Clone)" game object, but a script is trying to access it.
    You probably need to add a MeshRenderer to the game object "Quad(Clone)". Or your script needs to check if the component is attached before using it.
    UnityEngine.Renderer.get_material () (at <0f484ee213264ebc81764f2bdb13f359>:0)
    Ground.Start () (at Assets/Scripts/Ground.cs:16)


    I've tried using GetComponentsInChildren, I've tried Find... do I need to create the MeshRenderer separately somehow so it can be accessed like this? I'm sure this is something simple but would appreciate any tips :)