I am currently developing a simple 2D game to learn many things about game development.
Nevertheless, I have some issues to create a score system.
I want to update the score every second, and adding 1 point for each second, and stop the count when the game is over.
I start to write this
public class scoremanager : MonoBehaviour {
public int score;
public GUIText distance;
void Start () {
score = 0;
}
void Update () {
StartCoroutine(updateScoreManager(1f, 1));
distance.text = score.ToString();
}
IEnumerator updateScoreManager(float totalTime, int amount) {
score += amount;
yield return new WaitForSeconds (totalTime);
}
}
The issues are that nothing shows up in the GUIText so I don't know if it works. And how can I stop the count when the game is over?
I guess simple solution to your problem is InvokeRepeating
//Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
public void InvokeRepeating(string methodName, float time, float repeatRate);
So your transfomed code colud be
public class scoremanager : MonoBehaviour {
public int score;
public GUIText distance;
void Start () {
InvokeRepeating("updateScoreManager",1f,1f);
}
void Update () {
distance.text = score.ToString();
}
void updateScoreManager() {
score += 1;
}