- Home /
NullReferenceException: The prefab you want to instantiate is null.
I have a little problem with instantiate have a code as that ;
var prefab: Transform;
function Update () { if (Input.GetMouseButton(0)) { var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit;
if (Physics.Raycast(ray, hit)) {
Instantiate(prefab, hit.point, Quaternion.identity);
}
}
}
I have a Cube as prefab. When i play and clicked to any area i take this error.
NullReferenceException: The prefab you want to instantiate is null. UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) NewBehaviourScript 1.Update () (at Assets\NewBehaviourScript 1.js:12)
How can i insert my prefab where my clicked ?
Can you help me ?
Answer by cncguy · Jul 18, 2010 at 11:45 AM
Make sure your prefab is actually assigned in the inspector.
So if you try to instantiate a transform it will instantiate the the associated gameObject?
Yep. Same as if you make it a $$anonymous$$yScript reference, it'll instantiate the whole prefab, and give you back a reference to the $$anonymous$$yScript reference
How can i create a $$anonymous$$yscript reference ?
Answer by Mike 3 · Jul 18, 2010 at 11:53 AM
I've seen this a few times when you change the type used for the variable when you already had an object assigned in the inspector
Try set the prefab variable to another object in the inspector (or set it to None), then add your original object back in - it should add it back in properly