Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by darelhalgarth · Jul 19, 2018 at 03:30 PM · 2dcoroutineboolean

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;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Sgt_Spike · Jul 19, 2018 at 04:10 PM

Hey there. I can see your issue straight away. Don't worry, booleans and coroutines aren't too complicated. :D 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 :D

Good Luck!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darelhalgarth · Jul 19, 2018 at 05:35 PM 0
Share

Thank-you, the code now works and you've explained how everything works well! I'm going to have fun testing what else I can use booleans for. I think I'm also going to have to investigate while loops more too since those seem useful.

avatar image Sgt_Spike darelhalgarth · Jul 19, 2018 at 05:39 PM 0
Share

No problem, glad I could help! Booleans and while loops are both very useful and making a game would be hard without them so that's a good idea to look into them :D

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

246 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy Problem 2 Answers

Calling a bool from another script doesn't seem to be working 1 Answer

Issues with Scrolling Dialogue (repeating characters when printing text) 0 Answers

How would one go about creating a rolling HP text like Earthbound 0 Answers

Help with coroutine crashing unity 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges