Putting the code in update works perfectly, but I put it in Coroutine so that I can make it wait 3 seconds before the scene changes, it’s not working. Once the score reaches 530 the game just stays there. Any help would be amazing, thanks!
public class Score : MonoBehaviour
{
public static int scoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text>();
scoreValue = 0;
StartCoroutine(LevelUpdater());
}
// Update is called once per frame
void Update()
{
score.text = "Score: " + scoreValue;
}
IEnumerator LevelUpdater()
{
if (scoreValue == 530)
{
yield return new WaitForSeconds(3);
Application.LoadLevel("Start");
}
}
}
You are starting your coroutine in your start function. At the moment you have to reach 530 points in 3 seconds for this to work. Instead move your if statement into update function and start the coroutine when the 530 points is reached. Also you can create a bool which checks true if the score is reached, this way you can use it to stop the game and make sure the coroutine is started only once not on every frame.
Edit: I have to correct myself, the part about the 530 points in 3 seconds in my post was wrong. You have to have 530 points when your game starts not in 3 seconds for your present code to work. My bad