Search Unity

Searching an ArrayList of Instanced Prefabs possible?

Discussion in 'Scripting' started by AlexFormoso, Apr 20, 2013.

  1. AlexFormoso

    AlexFormoso

    Joined:
    Aug 12, 2012
    Posts:
    25
    Hello everyone!. I have this little script that, with declarations and stuff ignored, SHOULD, change the color of a prefab, instance it, add it to an ArrayList and then print the colors of every object in the array list.
    My problem is that, when I instance them and add them to the list, all is good, but when I search its colors, the instanced objects all gave NullReferenceExeption.

    I was hoping someone could look at it and spot something im missing here.
    Thanks in advance!

    Code (csharp):
    1.    
    2.  
    3. void Start () {
    4.       timer = 0;
    5.        ballList = new ArrayList();
    6. }
    7.  
    8.      void Update () {
    9.  
    10.         timer += Time.deltaTime;
    11.         if (ballList.Count < 10)
    12.          {
    13.             if (timer > 0.5)
    14.             {
    15.                 RandomizeColors(ballPrefab); //randomizes and assigns a material.
    16.                 clone = Instantiate(ballPrefab, transform.position, transform.rotation) as GameObject;
    17.               lista.Add(clone);
    18.                 timer = 0;
    19.             }
    20.         }
    21.         else
    22.         {
    23.             foreach (GameObject ballz in lista)
    24.             {
    25.                 Debug.Log(ballz.renderer.material.name); //NULL REFERENCE EXEPTION
    26.                 //here goes the code that searches for balls of the same color and deletes
    27.  them          
    28.               }
    29.         }
    30.  
    31.     }
     
  2. AlexFormoso

    AlexFormoso

    Joined:
    Aug 12, 2012
    Posts:
    25
    SOLVED im dumb as a wall can be
     
  3. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Also why you using an ArrayList, a type array (ie GameObject[]) or a Generic List is better.