I'm making a simplified inventory system for a 3d game.
When I try to pick up an object, I get the following error:
NullReferenceException: Object reference not set to an instance of an object
Here are the scripts I'm using:
Pick Up goes on the item - the error fires inside the Update method
public class PickUp : MonoBehaviour {
private playerInventory inventory;
public Transform woodIcon;
public Transform slot1;
public void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<playerInventory>();
}
public void Update()
{
if (inventory.slot1 == true) // <-- error fires on this line.
{
Destroy(gameObject);
Instantiate(woodIcon, slot1.position, woodIcon.rotation);
}
}
}
Player Inventory
public class playerInventory : MonoBehaviour {
public bool slot1;
public void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "wood")
{
}
if (slot1 == false)
{
slot1 = true;
}
}
}
Here is a screen shot showing my setup and the error:
