Search Unity

C# - Instantiate Prefab with random color

Discussion in 'Scripting' started by bigboybifdick, Sep 16, 2014.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hi there,

    This is my first post ! Nice to meet you all :)
    I'v been learning C# for a week, and i'm trying to instantiate prefabs with random colors, but can't figure out how to do it.

    Here is the code, a simple function that creates a grid of Gameobjects.

    Code (CSharp):
    1. public class Grid : MonoBehaviour {
    2.  
    3.     public Vector3 Grid_Size;
    4.     public GameObject Prefab_Sol;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.  
    9.    
    10.         CreateGrid ();
    11.  
    12.     }
    13.  
    14.     void CreateGrid () {
    15.    
    16.         for(int x = 0; x < Grid_Size.x; x++){
    17.             for(int z = 0; z < Grid_Size.z; z++){
    18.                 Instantiate(Prefab_Sol, new Vector3(x,0,z), Quaternion.identity);
    19.  
    20.             }
    21.  
    22.  
    23.         }}
    24. }
    25.  
    I would like to create randomly black or white gameobjects.
    I'v tried to do it with a if() function in CreateGrid(), but then all the instantiated prefabs have the same color...
    Can someone give me a clue ?

    Thanks a lot !
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    Code (CSharp):
    1. GameObject go = Instantiate(Prefab_Sol, newVector3(x,0,z), Quaternion.identity) as GameObject;
    2. go.GetComponent <MeshRenderer> ().material.color = newColor (Random.Range (0, 1), Random.Range (0, 1), Random.Range (0, 1), Random.Range (0, 1));
     
  3. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey Smitchell,

    Thanks for the tip.
    Something weird : your method always gives black gameobjects.
    If i replace Random.Range(0, 1) by Random.Range(0F, 1F), i can see the code is working (every Gameobject has a different color).

    What am i missing ?
     
    Last edited: Sep 16, 2014
  4. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    oh crap yeah, if you give random a int, it'll return a int. So what I wrote will always give you 0 or 1, yeah change it to 0.0f and 1.0f and it should work.

    What do you mean? Does it not work how you wanted it to?
     
  5. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Yep get it.
    Actually i would like get black or white gameobjects only. With float numbers i get plenty of ugly colors :)
     
  6. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you could do something like:

    Code (CSharp):
    1. Color col = Random.Range (0, 2) == 1 ? Color.black : Color.white;
    2. go.GetComponent<MeshRenderer>().material.color = col;
    (untested)
     
    BmxGrilled likes this.
  7. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Working perfectly !
    Now i'll be able to use the ?: operator, thanks a lot !
     
  8. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey again ! :)

    I'v tried replacing gameobjects i instantiate by imported .obj and i have this 'No MeshRenderer error'
    http://s15.postimg.org/cg2nod0uz/Capture_d_cran_2014_09_17_10_02_02.png

    When i check its components i can see 'Mesh Renderer':
    http://s30.postimg.org/3rfqq7bo1/Capture_d_cran_2014_09_17_10_05_50.png
    By the way, why are its components grey ?

    So i tried with a prefab. But then i noticed the mesh became a child of the prefab:
    http://s29.postimg.org/65ur86ghz/Capture_d_cran_2014_09_17_10_09_38.png

    Code (CSharp):
    1. GO.GetComponent<MeshRenderer>().material.color = col;
    doesn't work anymore, all the prefabs have the same color (default).

    So i thought i had to access the child and i replaced:

    Code (CSharp):
    1. GO.GetComponent<MeshRenderer>().material.color = col;
    by
    Code (CSharp):
    1. void CreateGrid () {
    2.  
    3.         for(int x = 0; x < Size.x; x++){
    4.             for(int z = 0; z < Size.z; z++){
    5.                 GameObject GO = Instantiate (Prefab_Sol, new Vector3(x,0,z), Quaternion.identity) as GameObject ;
    6.                 Color col = Random.Range (0, 2) == 1 ? Black : White;
    7.                 GO.GetComponentsInChildren<MeshRenderer>().material.color = col;
    8.  
    9.  
    10.  
    11.         }
    12.     }
    13. }
    14. }
    but then i have this error (no definition for material):
    http://s29.postimg.org/ntzzmgq9z/Capture_d_cran_2014_09_17_10_17_55.png

    Dont get it...

    Thanks a lot for your help !
     
  9. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    I need more information, unity has told you why it doesn't work. There isn't a mesh renderer, but lets figure out why there isn't one.

    When you instantiate the prefab, look in the hierarchy; is your mesh a child of the instantiated prefab? or?

    Screen shot it
     
  10. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Yep its a child of the prefab :
    http://s28.postimg.org/qk0352yel/Capture_d_cran_2014_09_17_10_36_08.png

    It finally works using:
    Code (CSharp):
    1. GO.transform.Find ("default").renderer.material.color = col;
    But i would like to understand why
    Code (CSharp):
    1. GO.GetComponentsInChildren<MeshRenderer>().material.color = col;
    didnt work ?

    I mean unity tell me there isnt a mesh renderer but i clearly see it on the object :
    http://s2.postimg.org/pixkqm26x/Capture_d_cran_2014_09_17_10_44_14.png
    Really dont get it :p
     
  11. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    because that is looking for all the components in all the children, it returns an array of components.

    You'd have to do something like:

    MeshRenderer childRendere = go.gameObject.GetComponentInChildren <MeshRenderer> () ... etc
     
  12. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    look at it carefully, it's different :)