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

public Transform orbPrefab;

You are instantiating a Transform.

Try Instantiating a Prefab or GameObject instead.