- Home /
About Instantiate
private ArrayList ListA = new ArrayList();
ListA.Add(Instantiate(PrefabA,transform.position, Quaternion.identity) as GameObject);
Debug.Log(ListA[0]);
I wonder why the console give me a "null" ~~ Thanks
I don't understand the code...You did specify the prefab you want to instantiate? Besides if you want to add something to an array, don't you use the 'push' command? then you say to instantiate ListA[something].
with a bigger explanation of what you want to do it will help us to help you hehe
Answer by BigBulle · Aug 01, 2011 at 09:09 PM
Try this :
ListA.Add((GameObject)Instantiate(PrefabA,transform.position, Quaternion.identity));
You should obtain an nice exception :-).
The 'as' operator return null if the cast fails. So I guess that your are not instantiating a game object...
Instantiate returns the same type declared in PrefabA. @BigBulle is right: probably PrefabA was declared as Transform or other type different from GameObject, what caused the problem. Declare PrefabA as GameObject:
public GameObject PrefabA; // I suppose you're using C#
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Checking if object intersects? 1 Answer
How to find index of a list? 3 Answers
How do we instantiate random sprites from an array... 1 Answer