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;
}
}
}