- Home /
instantiate problem
hello every1: i wrote a code like this:
var prefab : GameObject; function Update ()
{
if (Input.GetMouseButtonDown(0)) {
var prefab : Transform; var i : int; for(i=0;i<2;i++) { Instantiate (prefab); } } }
when we run the function we take this error message :
" NullReferenceException: The prefab you want to instantiate is null. UnityEngine.Object.Instantiate (UnityEngine.Object original) uretme.Update () "
how can i solve this problem?take you for attention...
Answer by SteveFSP · Jul 29, 2010 at 02:28 PM
For a summary, take a look at the following code comments:
var prefab : GameObject; // <--This value must be assigned. (Via editor or script.) function Update ()
{
if (Input.GetMouseButtonDown(0)) {
var prefab : Transform; // <--This variable is shadowing. Remove the line. var i : int; for(i=0;i<2;i++) { Instantiate (prefab); } } }
Some more details:
The "prefab" variable is declared in two locations in your code. The Transform version is shadowing the GameObject version. So when you call "Instantiate(prefab)" it will select the Transform version, which is always null.
After removing the line that declares the Transform version of the variable, if you are still having a problem, take a look at the game object that has the script attached. Does the prefab variable have an assignment? If not, you'll need to get one assigned.
Answer by spinaljack · Jul 28, 2010 at 02:23 PM
I haven't told unity what you want to appear.
Drag a prefab onto Prefab in the inspector.
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Crash when using GameObject.Instantiate 2 Answers
Instantiate a little before the end of the raycast or on screen 0 Answers
Enemy Instantiating one bullet 3 Answers