- Home /
Can't Call function in another script
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.
If you are using get component on this game object, just after destroying it, it's going to be null. You must have that script attached to another game object, and do getcomponent through the other object, like "otherGameObject.GetComponent..."
Answer by bubzy · Nov 11, 2014 at 07:04 AM
void OnTriggerStay2D() {
if (Input.GetMouseButtonDown (0)) {
Destroy (this.gameObject); //here! you are destroying the gameObject and then on the line below
GetComponent<ClueBehaviour>().foundItem(2); //you are trying to do something with the destroyed gameObject
//GameObject.Find ("Clue2").GetComponent<ClueBehaviour> ().isFound = true;
Debug.Log ("clicked");
}
}
its from experience, done it a couple of times :) ty for thumbs
Alright, fixing that sequencing got it to find the foundItem function properly, and I had to set a game object in the second script so that something could GetComponent. Now I'm running into the problem of being unable to call void functions. Is there a workaround here?
$$anonymous$$ake sure they are public void $$anonymous$$y$$anonymous$$ethod()
Also be sure to mark the answer as correct! (Little check mark in a circle)