- Home /
Question by
matthew 2 · Feb 22, 2011 at 11:27 AM ·
instantiateprefabbeginnernull
the prefab you want to instantiate is null
I have this problem where the unity editor debug panel says that "the prefab you want to instantiate is null". I tried to search why did this happen but i don't know why. I have a prefab called "cube" and a camera called "camera" NOT "Main Camera". this is the code that goes into the prefab, "cube" :
function Update () {
if (Input.GetAxis("spawn")>0){
var cube:Rigidbody;
cube=Instantiate(cube,transform.position+Vector3(0,0,4),Quaternion.identity);
};
}
and this is the code that goes into "camera" :
var x:float;
var z:float;
function Update () {
x=Input.GetAxis("Horizontal");
z=Input.GetAxis("Vertical");
transform.Translate(0,0,z/5);
transform.Rotate(0,x,0);
}
Thanks in advance.
Comment
Answer by Simple · Feb 22, 2011 at 11:34 AM
The main problem: you DONT have prefab. It is NULL because you have reinitialized it in next line:
var cube:Rigidbody; //problem in this line
cube=Instantiate(cube,transform.position+Vector3(0,0,4),Quaternion.identity);
First, try to add any object to your variable cube
for example:
var cube = GameObject.Find("AnyObjOnScene");
cube = Instantiate(cube,transform.position+Vector3(0,0,4),Quaternion.identity);
or make global var cube and attach object through Inspector
But how to make it a rigidbody? do you have to do this? var cube : Rigidbody = GameObject.Find("cube");
thanks!