- Home /
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 } } }
Answer by duck · Mar 16, 2010 at 08:49 PM
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).
Answer by Expendis · Mar 16, 2010 at 09:18 PM
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.
@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. :)
Answer by Cyclops · Mar 16, 2010 at 07:44 PM
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.
Yes, I have made sure of this..
http://i43.tinypic.com/2mmg407.jpg>
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 ins$$anonymous$$d?
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.
Answer by duck · Mar 16, 2010 at 08:05 PM
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.
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!
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.
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.