• 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 /
This question was closed Jun 11, 2013 at 06:22 AM by fafase for the following reason:

The question is answered, right answer was accepted

avatar image
Question by Shuvo0o · Jun 08, 2013 at 01:52 PM · errorguitextlabelblinking

GUI Text Blinking came up with one error.

The error came up saying "Assets/Ghost/Scripts/WarningText.cs(13,17): error CS0031: Constant value 1' cannot be converted to abool'"

Here's the code:

 using UnityEngine;
 using System.Collections;
 
 public class WarningText : MonoBehaviour {
     
     private bool textBlinking = false;
     
     void Start() {
         blinking();
     }
     
     IEnumerator blinking() {
         while(textBlinking) {
             textBlinking = true;
             yield return new WaitForSeconds(.5);
             textBlinking = false;
             yield return new WaitForSeconds(.5);
         }
     }
     
     void OnGUI() {
         GUIStyle style = new GUIStyle("label");
         style.fontSize = 40;
         GUI.color = Color.red;
         GUI.Label(new Rect(0, 0, Screen.width/2, Screen.height/2),
             "STAY AWAY FROM GHOSTS!", style);
     }
 }
 
Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image
Best Answer

Answer by fafase · Jun 08, 2013 at 02:52 PM

Hopefully I did not miss anything...:

 using UnityEngine;
 using System.Collections;
  
 public class WarningText : MonoBehaviour {
 GUIStyle style = new GUIStyle();
 private bool textBlinking = false;
 Rect rect;
 void Start() {
    rect = new Rect(0, 0, Screen.width/2, Screen.height/2);
    style.fontSize = 40;
    
    StartCoroutine(blinking());
 }
  
 IEnumerator blinking() {
 float timer = 0f;
 float endTimer = 0f;
 while(endTimer < 5f){
     while(timer < 1f) {
            timer += Time.deltaTime;
         yield return null; 
     }
     endTimer += timer; 
     timer = 0f;
     textBlinking = !textBlinking;
     yield return null; 
 }
 textBlinking = false;
  }
  
 void OnGUI() {
 
     if(textBlinking){
          GUI.color = Color.red;
          GUI.Label(rect,"STAY AWAY FROM GHOSTS!", style); 
     }
 }
 }


EDIT: Blinking method is reviewed and working.

 while(endTimer < 5f){

defines how long the message displays

 while(timer < 1f)

defines how long the message lasts on and off screen

Comment

People who like this

0 Show 4 · 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 Shuvo0o · Jun 08, 2013 at 02:57 PM 0
Share

I added that script into the first person controller and I ran the game, there was still nothing to display the warning message. What I need is to display the warning message for a few seconds at the beginning.

avatar image Shuvo0o · Jun 08, 2013 at 03:27 PM 0
Share

It still didn't display when I began the game. That's weird. I added that script into first person controller, is that fine?

avatar image Shuvo0o · Jun 08, 2013 at 03:42 PM 0
Share

Oh wait. I found the problem. It's actually the color that doesn't set to Red. It's black text and almost whole graphics of the game is black so that's why I couldn't see it.

avatar image Shuvo0o · Jun 08, 2013 at 03:47 PM 0
Share

I changed it to style.normal.textColor = Color.red; and it works. Thank you!

avatar image

Answer by Dave-Carlile · Jun 08, 2013 at 01:54 PM

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

The function needs to return IEnumerator rather than void.

Comment

People who like this

0 Show 3 · 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 Shuvo0o · Jun 08, 2013 at 02:14 PM 0
Share

Ok I changed that to IEnumerator and it ended up getting four new errors which come from while(1) and most codes in OnGUI

avatar image Dave-Carlile · Jun 08, 2013 at 02:16 PM 0
Share

Not much we can do to help unless we know what errors you're getting now. Did you look at the examples in the link?

avatar image Shuvo0o · Jun 08, 2013 at 02:20 PM 0
Share

I fixed other errors but one still exists. It says Assets/Ghost/Scripts/WarningText.cs(13,17): error CS0031: Constant value 1' cannot be converted to a bool'

avatar image

Answer by bubzy · Jun 08, 2013 at 02:34 PM

"while (1)" isn't a valid line

the while statement is looking for a true or false, thus it is saying you cannot convert a constant (1) to a true or false. 1 is always 1.

you may be better having a boolean variable called isBlinking and set that to true or false, then you can use while(isBlinking) or while(!isBlinking) depending on what you want.

incidentally, there are better ways of doing this, look up "delays"

Here :

 using UnityEngine;
 using System.Collections;
  
 public class WarningText : MonoBehaviour {
 GUIStyle style = new GUIStyle();
 private bool textBlinking = false;
 Rect rect;
 float delay = 0.2f;
 float currentDelay;
 
 
 
 void Start() {
 
    rect = new Rect(0, 0, Screen.width/2, Screen.height/2);
    style.fontSize = 40;
    GUI.color = Color.red;
 currentDelay = Time.time + delay;
 }
  
 void Update()
 {
     if (Time.time > currentDelay)
     {
     currentDelay = Time.time + delay;
         if(textBlinking)
         {
         textBlinking = false;
         }
         else if(!textBlinking)
         {
         textBlinking = true;
         }
     }
 
 }
 
 void OnGUI() 
 {
     if(textBlinking)
     {
         GUI.Label(rect,"STAY AWAY FROM GHOSTS!", style);
     }
 }
 
 }
Comment

People who like this

0 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 Shuvo0o · Jun 08, 2013 at 02:45 PM 0
Share

Ah okay. I fixed that and now it got four new errors again

"Assets/Ghost/Scripts/WarningText.cs(15,60): error CS1502: The best overloaded method match for `UnityEngine.WaitForSeconds.WaitForSeconds(float)' has some invalid arguments"

avatar image Shuvo0o · Jun 08, 2013 at 02:49 PM 0
Share

I changed 0.5 to 1, but then there's nothing to display at the beginning of the game.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Relate font size to screen size in GUISkin 2 Answers

Errors when adding a GUI.Label C# 2 Answers

GUI.label overlapping text 1 Answer

Custom Console in Application 1 Answer

Changing the size of a GUI label 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