NullReferenceException: Object Reference is not set to an instance of an object

Please guys I really need your help. I’m trying to get a script to reference something in another script and it’s giving me that error. Here’s the code.

public class Test : MonoBehaviour 
{
	public GameObject spherePrefab = null;
	public GameObject hand = null;
	public GameObject sphere;
	private int q = 0;
	private bool holding = false;
	Inventory getInv;

	void Start()
	{
		getInv = GetComponent<Inventory>();
	}

	//Update is called once per frame
	void Update () 
	{
		removeCrystal();
	}

	void removeCrystal()
	{
		for(int i = 0; i < getInv.Bag[q].quantity; i++)
		{
			if (getInv.Bag[q].quantity > 0) 
			{
				//Check if player is holding something
				//if false, check for input and create prefab in hand
				if(Input.GetButtonDown("Fire1")  holding == false)
				{
					sphere = GameObject.Instantiate(spherePrefab, transform.position, Quaternion.identity)as GameObject;
					sphere.rigidbody.isKinematic = true;
					sphere.transform.parent = hand.transform;
					holding = true;
				}
				
				//if true, add force
				else if(holding == true  Input.GetButtonDown("Fire1"))
				{
					sphere.rigidbody.isKinematic = false;
					sphere.rigidbody.AddForce(transform.forward * 2000);
					sphere.transform.parent = null;
					holding = false;
					
					Destroy(sphere, 5);
					getInv.Bag[q].quantity --;
				}

			}
		}
	}
}

Paste the entire error as it should contain more information than you’ve provided. Otherwise, we’re just guessing…

Jeff

using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour 
{
	public GameObject spherePrefab = null;
	public GameObject hand = null;
	public GameObject sphere;
	private int q = 0;
	private bool holding = false;
	private Inventory getInv;
	public int crystalCount;

	void Start()
	{

		getInv = GetComponent<Inventory>();
		crystalCount = getInv.Bag[q].quantity;
	}

	 //Update is called once per frame
	void Update () 
	{
		removeCrystal();
	}

	void removeCrystal()
	{
		if (crystalCount > 0) 
		{
			//Check if player is holding something
			//if false, check for input and create prefab in hand
			if(Input.GetButtonDown("Fire1")  holding == false)
			{
				sphere = GameObject.Instantiate(spherePrefab, transform.position, Quaternion.identity)as GameObject;
				sphere.rigidbody.isKinematic = true;
				sphere.transform.parent = hand.transform;
				holding = true;
			}
				
			//if true, add force
			else if(holding == true  Input.GetButtonDown("Fire1"))
			{
				sphere.rigidbody.isKinematic = false;
				sphere.rigidbody.AddForce(transform.forward * 2000);
				sphere.transform.parent = null;
				holding = false;
					
				Destroy(sphere, 5);
				crystalCount--;
			}

		}
	}
}

This whole thing happened because I didn’t declare a variable to hold the value of “getInv.Bag[q].quantity”. Once I did that, everything fell into place. The for loop had to be taken out because it was causing a bug.

Thanks…

If an admin wants to close this thread he can.

Left here for future game makers to learn from.