Reputation: 145
I want to run metod in the class from another class and both of them atached to different obj
Two classes. the first is:
public class NewScore: MonoBehaviour {
public void updateScoreBy( int intr) {
Debug.Log("intr+4"+intr+4);
}
}
and the second is:
public class NewPlus: MonoBehaviour {
public NewScore newscore;
public void OnTriggerEnter2D(Collider2D obj) {
newscore.updateScoreBy(+1);
}
}
When I run my code I recive an arror "NullReferenceException: Object reference not set to an instance of an object" and it points on newscore.updateScoreBy(+1); How can I fix it?
Upvotes: 0
Views: 1869
Reputation: 714
You didn't initialize newscore.
Change your NewPlus class like this so it will initialize newscore on construction.
public class NewPlus: MonoBehaviour {
public NewScore newscore;
public NewPlus(){
newscore=NewScore();
}
public void OnTriggerEnter2D(Collider2D obj) {
newscore.updateScoreBy(+1);
}
}
Upvotes: 0
Reputation: 7925
You need to initialize public NewScore newscore;
: drag drop the object that contains that script on that variable in the Inspector panel
Upvotes: 1
Reputation: 627
depending on what you want to do you could make the function static like:
public static void updateScoreBy(int intr)
{
Debug.Log("intr+4"+intr+4);
}
and call it with:
NewScore.updateScoreBy(1);
Upvotes: 0