It may not be an actual answer to your question, but you should not make idle use of static variables and classes. It would be a better idea, to have a GameManager-class, which knows all your other managers (has ‘public ScoreManager scoreManager;’ etc.), and let that handle things.
Your new GameManager class:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameManager : MonoBehaviour
public ScoreManager scoreManager = new ScoreManager();
}
Then everything you make (monsters etc.) that has to interact with something else in the game, just has to have a reference to the GameManager in them. If you want that in your ‘Slore’-class, you can do:
public GameObject gameManagerObject; // this is the variable, that will be visible in the Inspector
private GameManager gameManager;
void Awake(){
gameManager = gameManagerObject.GetComponent<GameManager>();
}
void OnClick () {
gameManager.scoreManager.score = 0;
}
Then in the Inspector, you simply drag the GameObject from your scene, which has the GameManager script on it, onto the GameManagerObject ‘slot’ of the Slore object.
Since Slore is supposed to be a button, there’s already an event-system, that can help you do this, so you don’t need to attach your own script to the button. Simply drag the new gameobject with the GameManager-script on it to a new OnClick-event on your button, choose the GameManager-script and method you want, and any parameters (if there are any; in the case of your ResetScore(), there are no parameters). See the video DiegoSLTS posted, to see how this is done.
Also, I’d recommend not updating text.text every update. You only need to update it whenever the score changes. Plus, having static variables like that score, is bad practice. You should consider making your ScoreManager manage the score, instead of just having it just be a container for the score variable.
First, you should remove ‘static’ from your score-variable.
Then make methods in your ScoreManager like this:
public void ApplyScoreChange(int change){
score += change;
UpdateScoreText();
}
and one like this:
public void SetScore(int newScore){
score = newScore;
UpdateScoreText();
}
and one like this:
public void ResetScore(){
score = 0;
UpdateScoreText();
}
and one like this:
public void UpdateScoreText(){
text.text = score;
}
Then, whenever you want to change the score, you know it’ll always be the same code doing the changes, and you can use these methods in your gameobjects, and even the OnClick() event on buttons.