- Home /
Trouble Instantiating Projectiles
Attempting to create a top down shooter where the player can shoot zombies that spawn. I'm having trouble getting the projectiles to work, however. I'm new to unity so bear with me.
public Rigidbody projectile;
if (Input.GetMouseButtonDown(0)) { if (bulletCount > 0) { anim.Play ("Strike");
ammo -= 1;
Rigidbody bullet = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
Physics.IgnoreCollision(bullet.collider, collider);
bullet.velocity = transform.TransformDirection(new Vector3(0, 0, bulletSpeed));
}
}
This is the player's function that creates the bullet.
public class bullet : MonoBehaviour {
public void OnTriggerEnter(Collider c) {
Debug.Log("zombie died");
GameObject.FindGameObjectWithTag("Player").GetComponent<Tom>().decreaseMobCount();
Destroy(c.gameObject);
Destroy(this.gameObject);
}
}
This is the bullet's function. Whenever I click in game, it tells me there is a nullreference exception, object reference not set to an instance of an object. I don't understand the issue here.
So does the error come from the above scripts?
also i imagine that you would want OnCollisionEnter rather OnTriggerEnter
There must be a problem with the way Im instantiating, I keep getting that same error.
I get the error on both lines right under creation of the bullet.
ive just tried the script my self and it worked... I Guess check that your Projectile you choose to duplicate has already a RigidBody and a collider.
Answer by screenname_taken · Apr 28, 2014 at 01:37 PM
Shouldn't you spawn a gameobject that has a rigidbody attached, and then apply the physics to that rigidbody? You are spawning a rigidbody directly i think. (Not familiar with C# yet.)
well that what that suppose to do, but i just means that "bullet" is referencing the rigibody of the new GameObject.
I thought that it was making a wild rigidbody out in the open. That's why it wasn't finding any colliders.
Your answer

Follow this Question
Related Questions
NullReferenceException 2 Answers
Material[] Object reference not set when instantiating 2 Answers
NullReference Exception when adding a class 2 Answers
NullReferenceException 1 Answer
Raycasthit NullRException 1 Answer