- Home /
Temporary GUI
I'm working on a small bit of code right now to have something show up on the screen but only for 3 seconds. I've gotten this far, but when I run the code it says "Script Error OnGUI () cannot be a coroutine." The debug code does not trigger either.
function OnGUI () {
if (Collect == true) {
GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
Debug.Log ("On");
yield WaitForSeconds (3);
Collect = false;
Debug.Log ("Off");
}
}
So, to anyone who knows what I'm doing wrong: Are you kind enough to share your knowledge with me?
Answer by ExTheSea · May 05, 2013 at 01:02 PM
You can't put yield into functions like OnGUI or Update but you could do something like this:
function OnGUI () {
if (Collect == true) {
GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
Debug.Log ("On");
}
}
function Only3Seconds(){
Collect = true;
yield WaitForSeconds (3);
Collect = false;
Debug.Log ("Off");
}
If you then want to start it you use this line:
yield StartCoroutine("Only3Seconds");
That's great and all, but I'm getting the message "OnGUI() cannot be a coroutine"
Are you starting the Coroutine in the Update Function? If that's the case try just
StartCoroutine("Only3Seconds");
without the yield.
I tried that and it's still being a pain from the very moment I start the test.
What do you mean by it's a pain? Do you get any errors? Also when do you start the coroutine?
You know what. Just try this: Delete your old script (or copy it somewhere before if you want) and make a new one and copy this inside:
static var Gem = 0; static var Collect : boolean = false;
function OnGUI () {
if (Collect == true) {
GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
Debug.Log ("On");
}
}
function Wait3Seconds(){
Start.Collect = true;
yield WaitForSeconds (3);
Start.Collect = false;
Debug.Log ("Off");
}
function Update(){
if(Input.Get$$anonymous$$eyDown("p"))
StartCoroutine("Wait3Seconds");
}
This is your code modified so that it works. I just created a Start.js and copied this in and it worked.
Answer by DryTear · May 05, 2013 at 04:25 PM
try this:
var Collect : boolean;
function OnGUI()
{
if(Collect == true)
//Do your Things here
}
function Update()
{
if(Collect == true)
{
RemoveGUI();
}
}
function RemoveGUI()
{
yield WaitForSeconds(3);
Collect = false;
}
Your answer

Follow this Question
Related Questions
Simple Animation for Unity3d 1 Answer
How to switch off two lights? 2 Answers
Adding Names 1 Answer
How to add aceleration to a sphere by pushing a key? 4 Answers