- Home /
Another yield not working problem
I've checked a lot of the questions about yields and tried a lot of different methods but can't get my code to work.
Here is my code:
private var sceneIndex : int; private var sceneName : String; private var sysFades : GameObject; sysFades = GameObject.Find("-main");
private var confirmSwitch : boolean = false;
private var confirmWin : Rect = Rect ((Screen.width/2)-65, (Screen.height/2)-30, 200, 60);
function OnGUI(){ if(confirmSwitch)confirmWin = GUI.Window (0, confirmWin, confirmSwitchFunction, "Switch to " + sceneName); }
function confirmSwitchFunction (windowID : int) { if (GUI.Button (Rect (10, 20, 50, 30), "Yes")) { switchScene(); } if (GUI.Button (Rect (70, 20, 50, 30), "No")) { confirmSwitch = false; } }
function webButtons(web_sceneIndex : int){ confirmSwitch = true; Debug.Log(sceneIndex); sceneIndex = web_sceneIndex;
if(sceneIndex == 0)sceneName = "Scene 0";
if(sceneIndex == 1)sceneName = "Scene 1";
if(sceneIndex == 2)sceneName = "Scene 2";
if(sceneIndex == 3)sceneName = "Scene 3";
}
function switchScene(){ Debug.Log("Fading Scene"); sysFades.SendMessage("sceneFadeOut"); confirmSwitch = false; yield WaitForSeconds (1); Debug.Log("Loading New Scene"); Application.LoadLevel (sceneIndex); }
I've tried starting switchScene as a coroutine and tried breaking down the code in switchScene into two functions either side of the yield. Basically, the code just stops running at the yield and the new scene never gets loaded. If I start the yield within the "Yes" button code the buttons stop appearing.
EDIT I've tried commenting out the SendMessage command as suggested and it still didn't work. Further to that I've unchecked all other scripts running in the scene and removed all scripts from all components.
I've also started a new scene in the same project with just a camera and an empty GameObject with code taken from the ref docs and other internet sources. This doesn't work. But I've switched to a different project and tried the code in that and it works fine. So, what could it be about my project that is causing it to not work?
Answer by LSnake2001 · Nov 15, 2011 at 08:18 PM
Thanks for the reply, aldonaletto. I'm not sure why this didn't work but I've fixed it by starting a new project and importing my previous project into that. I'm guessing the previous project was corrupt or something.
Answer by aldonaletto · Nov 06, 2011 at 06:37 PM
This code doesn't seem to be causing this issue. I suspect that the routine sceneFadeOut may be doing something wrong and screwing things up - it has one second to do that... Try to comment out the SendMessage instruction to test this, or edit your question and post the other script.
Your answer
Follow this Question
Related Questions
Getting an error when trying to yield 2 Answers
yield works, but yield WaitForSeconds does not? 1 Answer
Playing footsteps with interval 1 Answer
Load Level when dead for 1 second 2 Answers
Yield WaitForSeconds doesn't work, give syntax error 1 Answer