x


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 ?

more ▼

asked Jul 18 '10 at 11:41 AM

Vincenzo Albery gravatar image

Vincenzo Albery
49 20 23 34

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Make sure your prefab is actually assigned in the inspector.

more ▼

answered Jul 18 '10 at 11:45 AM

cncguy gravatar image

cncguy
1k 21 25 36

Transforms work fine (along with any other component)

Jul 18 '10 at 11:51 AM Mike 3

Oh ok - my bad:()

Jul 18 '10 at 12:00 PM cncguy

So if you try to instantiate a transform it will instantiate the the associated gameObject?

Jul 18 '10 at 12:01 PM cncguy

Yep. Same as if you make it a MyScript reference, it'll instantiate the whole prefab, and give you back a reference to the MyScript reference

Jul 18 '10 at 01:09 PM Mike 3

How can i create a Myscript reference ?

Jul 18 '10 at 06:40 PM Vincenzo Albery
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 18 '10 at 11:53 AM

Mike 3 gravatar image

Mike 3
30.5k 10 65 252

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1670
x1253

asked: Jul 18 '10 at 11:41 AM

Seen: 2725 times

Last Updated: Jul 18 '10 at 11:41 AM