I’ll keep this brief. I’m building an inventory system with a friend. The code that registers the item in the inventory of the player is thus
public void foundItem(int symbolNum){
//Iterates through all the clue items based off the length of images there are
//If the itemNum = images[ itemNum - 1] you have found the item
//Set the item that you have found to isFound = true
//Set the newly found item image to a visible transpency
print ("Looking for item# "+symbolNum);
//If the clue value is the same as symbol value, proceed
GameObject[] clueSymbolGOs = GameObject.FindGameObjectsWithTag("ClueSymbol");
//We are now looking for the clueItem i that matches s (symbol number)
foreach(GameObject clue in clueSymbolGOs){
if(clue.GetComponent<ClueBehaviour>().getClueSymbol() == symbolNum){
print ("Found clue symbol game object # "+ symbolNum);
//clue.GetComponent<GUITexture>().texture = symbolHolder;
//this.gameObject.GetComponent<GUITexture> ().texture = images[itemNum];
clue.GetComponent<ClueBehaviour>().isFound = true;
clue.GetComponent<GUITexture>().color = new Color(1f, 1f, 1f, 1f);
}
}
}
Obviously that’s not the entire code, but that’s the segment I’m working with. I need to make it so that when I find an item, it runs this function in order to add the item to the inventory. The current code I have for that is thus
void OnTriggerStay2D() {
if (Input.GetMouseButtonDown (0)) {
Destroy (this.gameObject);
GetComponent<ClueBehaviour>().foundItem(2);
//GameObject.Find ("Clue2").GetComponent<ClueBehaviour> ().isFound = true;
Debug.Log ("clicked");
}
}
The way this works is that when the player is in contact with the item, clicking on it will destroy the item on the map and change the item in the inventory to be registered as found. However, I am getting an error stating “Object Reference is not set to an instance of an object” when the item is picked up. The item will delete as is proper, but the isFound Bool is unchanged. What am I doing wrong? THanks in advance.