Warning with FPS Tutorial

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 );
	}
}

[/quote]

Looks like the ‘projectile’ variable is unassigned.

I guess I don’t understand? The script is ‘Projectile’ and is associated with a prefab.

Instantiate(projectile,transform.position, transform.rotation);

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.

I’ve come across the same issue, but only in Part 2 after I’ve got the RocketLauncher strapped in.

The Rocket Launches

The Smoke Follows

There’s a nice explosion when it hits

I get the NullReferenceException at Rocket.js:16

The Projectile’s been set in the inspector, I’ve removed it and added it back several times to no effect.

I’m sure I’ve missed something in the tutorial, but you know how it is, the solution just stares back defiantly daring you to see it.

Any suggestions on how I might track down what’s happening?

Note: I’m running on Snow Leopard, if that makes a difference.
Note: I have not modified the Rocket.js script from the downloaded tutorial.

Cheers

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.

Anyhow, see how you go.

Dave

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.

Not sure if that’s of use to you?

Dave.

Sounds like a great approach - kind of like working in mini sprints in an Agile project. I’ll definitely use this approach. Thanks for the help.

Leo