- Home /
The question is answered, right answer was accepted
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);
}
}
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
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.
It still didn't display when I began the game. That's weird. I added that script into first person controller, is that fine?
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.
I changed it to style.normal.textColor = Color.red; and it works. Thank you!
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.
Ok I changed that to IEnumerator and it ended up getting four new errors which come from while(1) and most codes in OnGUI
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?
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'
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);
}
}
}
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"
I changed 0.5 to 1, but then there's nothing to display at the beginning of the game.
Follow this Question
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
Ngui Text Vertical Scrollbar? 1 Answer