x


The prefab you want to instantiate is null.

In my scene, I have a prefab of a particle (also tested it with a plain sphere to no avail) that I am trying to instantiate at the collision of a ray (as a bullet explosion).

I figured I would get this error if the prefab didnt contain any objects but that is not the case. (The prefab icon is blue)

This is just another flaw in my code, I bet, that results from my lack of experience. Thanks in advance for helping!

Ive attached this script to the object that is shooting the ray that results in the collision.

var gun : Transform;
var layerMask = 1 << 8;
var loc : Vector3; 
var bulletspark : Transform;

function Update () {

//position of the gun in the world
var gunPosition : Vector3 = gun.position;
// distance calculation assumes camera has 0, 0, 0 rotation
var distanceFromCamera : float = gunPosition.z - camera.main.transform.position.z;
var screenPos : Vector3 = Input.mousePosition;
screenPos.z = distanceFromCamera;
// calculate where in the game-plane the bullet must go to
var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(screenPos);
// calculate bullet direction...
var dir : Vector3 = (worldPos - gunPosition).normalized;

if (Input.GetButtonDown ("Fire1")) {
var hit: RaycastHit;
    if (Physics.Raycast (gunPosition, dir, hit)) { //cast the ray
    var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal); //determine the rotation of the collision
    loc=hit.point; //determined the location of the collision
    loc.z = 0;
    Debug.DrawRay (gunPosition, dir);//debug
    print(rot);//debug
    print(loc);//debug
    Instantiate(bulletspark, loc, rot);//create sparks
    }
}
}
more ▼

asked Mar 16 '10 at 06:40 PM

Expendis gravatar image

Expendis
23 2 2 11

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

4 answers: sort voted first

From everything you've described so far, it would appear that the particular instance you're examining is set up entirely correctly.

Which leads me to think, could it be that you've accidentally dropped that script onto some other GameObject in your hierarchy too, without realising? If so, it may be that instance which is throwing the error (as opposed to the one you're inspecting, which seems fine).

more ▼

answered Mar 16 '10 at 08:49 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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

Thanks so much for your answers!

My problem is solved.

The problem was actually that I was dragging the Bulletspark prefab into the script and not the instance of the script in the object that fires the raycast.

I predicted that this was a problem related to a misunderstanding in the interface.

Once again thanks for your answers. This community is awesome.

more ▼

answered Mar 16 '10 at 09:18 PM

Expendis gravatar image

Expendis
23 2 2 11

@Expendis, it's nice to say thanks, but for future reference, it should go into a comment in another Answer, not as a separate Answer. Also consider up-voting and check-boxing the best answer (which in this case, appears to be Duck's - he needs the points, anyway. :)

Mar 17 '10 at 04:18 PM Cyclops
(comments are locked)
10|3000 characters needed characters left

This is checking the obvious, but, you're sure that bulletspark is correctly pointing to the Prefab, in the Editor window? You did a drag/drop, or used the dropdown menu, and it's connected to the public script variable? I've had times when I accidently disconnected a variable without noticing.

more ▼

answered Mar 16 '10 at 07:44 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

Yes, I have made sure of this..

Mar 16 '10 at 07:54 PM Expendis

Yes, the picture shows bulletspark as connected - but gun is null. I don't see it in the script above, is it also instantiated, and could the error message be from that code instead?

Mar 16 '10 at 08:20 PM Cyclops

Hm, yes you're not trying to instantiate the gun, but you are using it to fill the variable "gunPosition". If 'gun' isn't assigned, you should get a null reference error though, not a Prefab is null error. Strange.

Mar 16 '10 at 08:51 PM duck ♦♦
(comments are locked)
10|3000 characters needed characters left

Have you changed the type of the variable 'bulletspark' since you made the drag-reference? For example, perhaps it used to be:

var bulletspark : GameObject;

...or something else, when you made the drag-reference, and then maybe you changed the type to:

var bulletspark : Transform;

If so, you have to re-drag the reference even though it appears to still be linked. I can't remember off the top of my head whether this generates the error that you describe, or a different error, but it's one more thing to check.

If you can't remember whether you changed it, just try re-dragging the reference anyway and see if it fixes it. If it does, that was most likely the cause of the problem.

more ▼

answered Mar 16 '10 at 08:05 PM

duck gravatar image

duck ♦♦
41k 92 148 415

I havent changed the variable type but to be sure, I dragged it in again and Im still getting the same error.

Thanks for your help so far!

Mar 16 '10 at 08:14 PM Expendis

@Duck, that's a good point - why is it a Transform, anyway? A Prefab is really a GameObject. If you didn't change it, @Expendis, might try it.

Mar 16 '10 at 08:22 PM Cyclops

I changed it to a GameObject, and to make sure that the glitch you mentioned wasnt occurring, @Duck, I dragged the prefab back onto Bulletsparks on the script. ...To no avail.

Same error, same place in the script.

I greatly appreciate your help in this! Thanks everyone.

Mar 16 '10 at 08:43 PM Expendis

It should work as a transform, if you didn't change it after the drag. You can instantiate a prefab from a reference to any type of component attached to it. Whichever you use, the entire prefab will be instantiated in its complete form.

Mar 16 '10 at 08:44 PM duck ♦♦
(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:

x1683
x1536
x1260
x198

asked: Mar 16 '10 at 06:40 PM

Seen: 6468 times

Last Updated: Mar 16 '10 at 06:40 PM