NullReferenceException: The prefab you want to instantiate is null.
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation)
MissileLauncher.Update () (at Assets\WeaponScripts\MissileLauncher.js:8)
Completed the first FPS tutorial and everything works fine. However, I’d like to know how to get rid of this warning. Any ideas on what I missed doing?
Here’s the script…
var projectile : Rigidbody;
var speed = 20;
function Update ()
{
if( Input.GetButtonDown( "Fire1" ) )
{
var instantiatedProjectile : Rigidbody = Instantiate(projectile,transform.position, transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0,0,speed));
Physics.IgnoreCollision( instantiatedProjectile.collider,
transform.root.collider );
}
}
This instantiates whatever ‘projectile’ refers to. However, there is nothing in projectile, so it throws ‘The prefab you want to instantiate is null’. You have to assign a reference to something to the projectile variable, either through inspector ( http://unity3d.com/support/documentation/Images/manual/Learning%20the%20Interface-31.jpg ) or via code.
Without even looking at your code, as a suggestion, I would try re-creating the projectile prefab or whichever prefab is giving you grief (should only be a quick process for that FPS tutorial).
I’ve found that stuff won’t work for me for ages then when I start from scratch, low and behold it works perfectly first go after starting over. This seems to happen every now and again for me when it comes to prefabs.
Thanks for the input - I haven’t recreated the projectile prefab, but your suggestion has helped me through one or two other hiccoughs. I’ll go back later and try to get this fixed up.
I addition to rebuilding and printing to logs, do you have any other debugging methods or tools you find useful? If there’s a post on this I apologize - I link would be fabulous!
It’s not so much a debugging technique, but when I’m unsure of exactly how to get something working, I tend to create a new prefab or whatever it might be and number it eg. playerChar01, where I add something, check it works as expected then once I’m happy with it, I will make a copy of playerChar01 and name it playerChar02 adding the next small step. (Pretty much like a programmers development cycle). In this way I can usually go “well this didn’t work, or its now broken and the original thing doesn’t work now” so I’ll copy playerChar01 again and try again.
It just saves you finding somewhere down the track that your prefab is buggered up and you don’t know where it broke or whatever which is horribly frustrating and a lot of work to sift through it all to find the problem.