- Home /
FPS Tutorial part 2 - please help
I'm following the FPS tut, and i have a problem: when i shoot my rocket launcher, the rocket is supposed to explode when it hits something. instead, it just bounces off and flies around for 2-4 seconds before disappearing. there's also an error that says
NullReferenceException: The prefab you want to instantiate is null. UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) Rocket.OnCollisionEnter (UnityEngine.Collision collision) (at Assets\WeaponScripts\Rocket.js:18)
this is the Rocket.js Script:
// The reference to the explosion prefab var explosion : GameObject; var timeOut = 3.0;
// Kill the rocket after a while automatically function Start () { Invoke("Kill", timeOut); }
function OnCollisionEnter (collision : Collision) { // Instantiate explosion at the impact point and rotate the explosion // so that the y-axis faces along the surface normal var contact : ContactPoint = collision.contacts[0]; var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal); Instantiate (explosion, contact.point, rotation); // And kill our selves Kill ();
}
function Kill () { // Stop emitting particles in any children var emitter : ParticleEmitter= GetComponentInChildren(ParticleEmitter); if (emitter) emitter.emit = false;
// Detach children - We do this to detach the trail rendererer which should be set up to auto destruct
transform.DetachChildren();
// Destroy the projectile
Destroy(gameObject);
}
@script RequireComponent (Rigidbody)
have you assigned the explosion GameObject by dragging it on to the exposed var slot in the inspector?
yes, i even tried different explosion prefabs. the error is still there and it stll doesent work
Answer by 3dDude · Aug 13, 2010 at 10:48 PM
when you click the rocket prefab in your project folder in the rocket script make sure that the explosion var is set to something......... there should be a explosion prefab in your standard assets drag this prefab into the empty var slot.....
i did that, even trying different explosion prefabs. the error is still there and it stll doesent work
hhhhhmmmmmmm i would try just comparing the finished version and the one you working on..... cuz theres got to be a problem somewhere ;)