- Home /
Help with my code ArgumentException: The prefab you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message)
So I am making a game that will spawn a random game object out of a 10 game objects in an array. The game object is an image put in a empty game object. I don't know whats wrong with my code but can someone help please? I keep getting this error: ArgumentException: The prefab you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message)
#pragma strict
var obj : GameObject[];
obj = new GameObject[10];
function Start () {
}
function Update () {
var randomObj = Random.Range(0,obj.length);
var randomNum : int;
randomNum = randomObj;
var theObj = obj[randomNum];
Instantiate(theObj, Vector3.zero, Quaternion.identity);
}
Answer by zharik86 · Jan 17, 2015 at 07:56 AM
Initialization your array in Inspector (as public) or in Start():
var obj : GameObject[];
function Start () {
obj = new GameObject[10];
//Set up for each element game object
obj[0] = GameObject.Find("myGameObject");
//And etc
}
I hope that it will help you.
Your answer

Follow this Question
Related Questions
Unitron error? 1 Answer
Referencing instantiated player in camera's Start() 2 Answers
Trying WaitForSeconds before Instantiating 2 Answers
How to Instantiate prefab as child? (Java) 1 Answer
Prevent instantiating on collider 1 Answer