Hi there! I’m trying to make a simple script, that will add a gameobject to my list, when the player collides with it and presses the use key. However I keep getting the “Object reference not set to an instance of an object” error. It points to the second script, line 22. Any help?
First script PlayerInventory.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerInventory : MonoBehaviour {
public List<GameObject> myInventory;
void Start()
{
myInventory = new List<GameObject>();
}
public void AddToInventory(GameObject newItem)
{
myInventory.Add (newItem);
Debug.Log ("Added");
}
}
Second script GameObjectToInventory.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GameObjectToInventory : MonoBehaviour {
private PlayerInventory playerInventory;
void OnTriggerStay(Collider otherObject)
{
if(Input.GetButtonDown("Use")){
PlayerInventory playerInventory = GetComponent<PlayerInventory>();
playerInventory.AddToInventory(this.gameObject);
Destroy (this.gameObject);
}
}
}