How to erase message after 3 seconds

I’m trying to test out some simple concepts in Unity (2d) since I’m new to coding. I’m trying to code something so that if a boolean is true then after 3 seconds a thing will happen and the boolean will become false. Below is the code, which is attached to a camera. The current error I’m getting is that “The variable ‘MessageDisplayed’ is assigned but its value never used.” I’ve tried looking up information about creating a boolean variable without any straightforward answer, and coroutines are just going straight over my head. Everything else is working fine. Any advice about how to code what I’m trying to do and links to guides explaining booleans or coroutines would be appreciated!

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class playerMonocoins : MonoBehaviour {

    public int Monocoins;
    public Text MonocoinsDisplay;
    public Text MessagesDisplay;
    public float DisplayTime = 5f;
    bool MessageDisplayed = false;

	// Use this for initialization
	void Start ()
    {
        Monocoins = 100;
        MonocoinsDisplay.text = "Monocoins:" + Monocoins;
        MessagesDisplay.text = "You have started the game.";
        bool MessageDisplayed = true;
        StartCoroutine(MessageDisplayedUpdate());
	}
	
	// Update is called once per frame
	void Update () {
        MonocoinsDisplay.text = "Monocoins:" + Monocoins;
	}

    public void addmonocoins(int monocoinsToAdd)
    {
        Monocoins += monocoinsToAdd;
        MessagesDisplay.text = "You gained 10 monocoins";
        bool MessageDisplayed = true;
    }

    public void subtractmonocoins(int monocoinsToSubtract)
    {
        if (Monocoins - monocoinsToSubtract < 0)
        {
            Debug.Log("You Don't have enough monocoins");
            MessagesDisplay.text = "You don't have enough Monocoins";
            bool MessageDisplayed = true;
        }
        else
        {
            Monocoins -= monocoinsToSubtract;
            MessagesDisplay.text = "You lost 10 monocoins";
            bool MessageDisplayed = true;
        }
    }

    IEnumerator MessageDisplayedUpdate()
    {
        if (MessageDisplayed == true)
        {
            yield return new WaitForSeconds(3);
            MessagesDisplay.text = "";
            bool MessageDisplayed = false;
        }
    }
}

Hey there. I can see your issue straight away. Don’t worry, booleans and coroutines aren’t too complicated. :smiley: First thing you should know about a coroutine is that it only executes once until it is called again. You starting the coroutine from your Start() method means it is only going to be executed once as soon as this class becomes active. Now with booleans, they work just like any other variable. You can declare them as public or private in the class and then use that boolean anywhere else in that class. Everytime you put ‘bool MessageDisplayed = something’ you are creating a new boolean rather than using the same one. That will also explain why you are getting the error saying that the boolean is never used. To solve that, simply just change the code on line 13 to ‘private bool MessageDisplayed = false;’, and everytime you use this boolean, use ‘MessageDisplayed = something’ rather than ‘bool MessageDisplayed = something’.

That should now sort out your boolean and coroutine so now you just need to repeat the coroutine. The easiest way of doing this is by using a ‘while’ loop. Go ahead and change your IEnumerator to this:

     IEnumerator MessageDisplayedUpdate()
     {
         while(true)
         {
             if (MessageDisplayed == true)
             {
                 yield return new WaitForSeconds(3);
                 MessagesDisplay.text = "";
                 MessageDisplayed = false;
             } else
             {
                 yield return null;
             }
         }
         
     }

What this will do is repeat your code inside of the while loop as long as true is equal to true, which it is of course :D. Also, make sure you add an ‘else’ to your if statement and put ‘yield return null’. If you don’t do this then Unity will crash when MessageDisplayed = false as it’s an infinite loop.

And just as a friendly warning, make sure you save your project and scenes before trying it. While loops have crashed Unity for me in the past when I make a small mistake and then I rage about all the work I’ve lost all day :smiley:

Good Luck!