- Home /
coroutine or yeild for OnGUI?
Hi all,
I have a simple toolbar that I am using to load the different levels of my application; the problem I am having is that when I attach the application.loadlevel script to a specific level's main camera, the other scripts in that given level stop functioning - however, the other levels function perfectly.
After doing some research, I thought that maybe this had to do with the execution order of the OnGUI script vs. the other Update scripts...but I don't know. Here's my application.loadlevel script:
var toolbarInt = 0;
var toolbarStrings : String[]= ["ReLoad", "Level 1", "Level 2", "Level 3"];
var mySkin : GUISkin;
function OnGUI () {
GUI.skin = mySkin;
toolbarInt = GUI.Toolbar (Rect (Screen.width - 300, Screen.height - 40, 290, 30), toolbarInt, toolbarStrings);
if (toolbarStrings[toolbarInt] == "ReLoad") {
Application.LoadLevel (0);
}
if (toolbarStrings[toolbarInt] == "Level 1") {
Application.LoadLevel (1);
}
if (toolbarStrings[toolbarInt] == "Level 2") {
Application.LoadLevel (2);
}
if (toolbarStrings[toolbarInt] == "Level 3") {
Application.LoadLevel (3);
}
}
Do I need to make the GUI part a coroutine (ie, make it run after the update scripts are called)? If so, how do I do this?
Thanks in advance.
Thank you! So just to be sure I get it right, when I create the new string: -Do I just have an empty return field, for the "Do Nothing" button? -How do I make it the default option? -What if I don't want another button on the toolbar though? Again, thank you so, so much for your help.
This question was answered twice, but the correct answer disappeared when I was deleting my own misplaced answer (which should've been a comment). Sorry all.
Basically, the answer was that there needed to be another string called "Default" which allow time for the OnGUI scripts to be called before loading a level; otherwise Application.LoadLevel (0) is called multiple times. I don't think I did the original person answering the question justice here.
Sorry! Something weird happened there. Anyway, I've undeleted my answer (somehow).
Answer by syclamoth · Sep 28, 2011 at 01:55 PM
None of the above- the problem here is that your GUI script will be calling Application.LoadLevel(0) every single GUI step! You need to create another string in your toolbar, which says "Do Nothing" or "Choose Option" and just sits there and does nothing, and is also the default option. This way, the GUI will wait until you have made your choice before doing anything!
Your answer
Follow this Question
Related Questions
Yield WaitForSeconds doesn't work, give syntax error 1 Answer
Wait For Seconds to Load level C# 2 Answers
C# Wait for Coroutine 3 Answers
How to set timer for WWW helper? 1 Answer
Execution manner of co routine 2 Answers