- Home /
AddForce NullReferenceException
Hey guys,
I have an issue with trying to get my object to fire a weapon. It seems that everytime I try to fire the weapon, the bullet is instantiated but the AddForce line gives an error saying it is a "NullReferenceException". I have debugged the error and it seems like the variable "shot" was never set to any object (it was shown as null). However, in the code, it is CLEARLY set to the orb prefab right before it is being used. Anyone know what the issue could be? Any help would be greatly appreciated.
Here is my code (C#):
public class GrabObject : MonoBehaviour {
public int bGrabObj = 0;
public int bGrabOrb = 0;
public Transform orbPrefab;
void Update (){
if(Input.GetMouseButton(1) && bGrabOrb == 1)
{
GameObject.FindGameObjectWithTag("Arm").GetComponent<GunProperties>().Disappear();
bGrabOrb = 0;
}
else if (Input.GetMouseButtonDown(2) && bGrabOrb == 1)
{
GameObject shot = Instantiate(orbPrefab, transform.position + (transform.forward * 2), transform.rotation) as GameObject;
shot.rigidbody.AddForce(shot.transform.forward*5000); // Code Breaks Here
bGrabOrb = 0;
}
}
}
Please format your code. If you don't know how watch the tutorial video
Answer by meat5000 · Sep 15, 2013 at 07:58 AM
public Transform orbPrefab;
You are instantiating a Transform.
Try Instantiating a Prefab or GameObject instead.
Oh man THAN$$anonymous$$ YOU SO $$anonymous$$UCH. It works now!
Your answer
Follow this Question
Related Questions
Instantiate giving a NullReferenceError 1 Answer
OnCollisionEnter not being called, between a Rigidbody and a Box Collider 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Instantinate NullReferenceError problem 0 Answers