Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C#, Instantiate() and Lists

Discussion in 'Editor & General Support' started by amanset, Jan 26, 2014.

  1. amanset

    amanset

    Joined:
    Oct 19, 2013
    Posts:
    11
    Sorry if this is really obvious and basic, but I have been searching and searching and everything I see seems to say this should work, but it doesn't.

    I am trying to instantiate a selection of GameObjects and put them in a list. They are all from the same prefab (my plan is to change properties of each object with a SendMessage later). The Instatntiation works. I can see the objects, but I just can't seem to access them from the script that does the Instantiate().

    This is what I have:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class MBSetupCircles : MonoBehaviour {
    6.  
    7.     public GameObject circlePrefab;
    8.     public List<GameObject> myList = new List<GameObject>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         for (int i=0; i<5; i++) {
    14.             GameObject circle = Instantiate(circlePrefab) as GameObject;
    15.             myList.Add(circle);
    16.         }
    17.  
    18.         foreach(GameObject obj in myList) {
    19.             Debug.Log(obj.tag);
    20.         }
    21.    
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.    
    27.     }
    28. }
    Nothing I do, either right after the Instantiate() or, as in the example, looping through the elements of the list allows me to access the objects. The objects themselves works fine, I have them moving and interacting with the environment using the scripts connected to them in the prefab. It is just here that I cannot do anything. I just get this error:

    Clearly there is something very basic I am missing, probably to do with not having the reference to the instantiated object, but I just can't get it to work.

    Help please, somebody!
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    That looks like it should work. I'm guessing the reason it's null is because it fails to cast at the "as GameObject" part when you instantiate. Using the "as" keyword will try to cast to the class you supply or return a null reference if it can't cast. Try changing that line to this:

    Code (csharp):
    1.  
    2. GameObject circle = (GameObject) Instantiate(circlePrefab);
    3.  
    When you cast this way, it will instead throw an exception if it can't cast, instead of just assigning null. At least this way, you might get a more helpful error to see what's going on, like "Can't cast an instance of type xxxxx to type GameObject". Maybe the type of your prefab is getting messed up somehow.
     
  3. amanset

    amanset

    Joined:
    Oct 19, 2013
    Posts:
    11
    Thanks, you are exactly right. I just fixed and came here to write that and saw your answer!

    I was clicked around the project looking for anything and saw that for the prefab it said "Type Mismatch" instead of the prefab name. I dragged the prefab back onto it and it worked. No idea how it got screwed up but now I have learnt to check these things.

    Thanks for taking the time to reply!