- Home /
How to call an external method?
Hi, I have two scripts GameManager and GoldCatcher. GameManager holds most of the score-related computing while GoldCatcher serves only to detect collision. Now, I want to do this: GoldCatcher detects collision->sends some sort of message to GameManager->GameManager executes function ComputeGold.
I tried this:
public GameManager gamemanager;
void Start ()
{
gamemanager = GetComponent<GameManager>();
}
void OnTriggerEnter2D(Collider2D coin)
{
Debug.Log("collision2");
gamemanager.ComputeGold();
}
but that gives me an error in Unity: NullReferenceException: Object reference not set to an instance of an object. It gives me the same error even without the Start function.
Anyone knows how I should approach this? Please help.
Answer by applemaniac · Jun 01, 2015 at 06:02 AM
Another way to achieve this is to use static function. You make your ComputeGold method static and you delete the 6 first lines of your GoldCatcher script.
The error can also come from your ComputeGold method.
Answer by tanoshimi · Jun 01, 2015 at 06:08 AM
Seeing as you've made gamemanager public
, just get rid of the Start() function and drag the game manager script into the appropriate slot of the GoldCatcher component in the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Use UI Button from other Script 2 Answers
Receiving windows message in Unity 0 Answers
Passing a class instance to a method 1 Answer