Function Problem, has been 3 hours tried to fix this.. HELP!
So here's my case.
I want to update score after this enemy killed. So I put this code.
public ScoreDisplay scoreDisplayx;
public int pointsEarnedToKill = 100;
Well, I am trying to access AddScore() function in ScoreDisplay.cs script.
public void DealDamage (float damage){
currentHealth -= damage;
slider.value = currentHealth;
if(currentHealth<0){
scoreDisplayx.AddScore(pointsEarnedToKill);
Destroy(gameObject);
}
}
Here's the ScoreDisplay.cs looks like.
public Text scoreText;
private int score = 0;
public void AddScore(int amount){
score += amount;
UpdateScore();
}
public void UpdateScore(){
scoreText.text = score.ToString();
}
I have assigned the Text object but seems it doesn't work. It never execute the destroy gameobject. I have tried to Debug Log after AddScore function but never showed up.
Instead, it continously give me error "Object reference not set to an instance of an object" in this line:
scoreDisplayx.AddScore(pointsEarnedToKill);
Any help would be apreciated much. Thanks!
Answer by corn · Jan 18, 2016 at 11:47 AM
This means that scoreDisplayx
is null. You need to get a reference to this AddScore instance, via drag and drop in the Inspector or GetComponent.
If both scripts are on the same GameObject, you can just use :
void Awake()
{
scoreDisplayx = GetComponent<ScoreDisplay>();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612060635im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a textbox like they have in pokemon 1 Answer
UI Canvas Anchor points are stuck in the bottom left corner and disabled when in overlay mode. 1 Answer
How To Load TTF Font From External File 0 Answers
How to make Textmesh Pro effects on only one text object? 1 Answer
OnGUI behind UI? 0 Answers