- Home /
If statement inside Coroutine firing without meeting the conditions
I have two coroutines, one for Loading and another for Saving, both corutines are pretty much the same, in fact I copies and pasted one to create another, yet one of them does stuff without meeting the conditions (in this case, pressing the "attack" button), the loading coroutine just loads the first save slot without me ever pressing the attack button.
I cant post the whole code as is quite large but here are the coroutines
IEnumerator LoadCo(){
GameObject GameManager = GameObject.FindGameObjectWithTag("GameManager");
SaveSystem _SaveSystem = GameManager.GetComponent<SaveSystem>();
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
_SaveSystem.Load(MenuString);
yield return new WaitForSeconds(1);
}
}
IEnumerator SaveCo(){
GameObject GameManager = GameObject.FindGameObjectWithTag("GameManager");
SaveSystem _SaveSystem = GameManager.GetComponent<SaveSystem>();
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
_SaveSystem.Save(MenuString);
yield return new WaitForSeconds(1);
}
}
And a switch I have, this is all the relevant code as the rest is OnGUI stuff.
switch(MenuIndex){
case -1:
MenuIndex = 3;
break;
case 0:
if (PauseMenuInt == 0){
MenuString = "Resume";
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
_GameManager.MenuBool = false;
}
}
if (PauseMenuInt == 1){
MenuString = "Slot 1";
StartCoroutine (SaveCo());
}
if (PauseMenuInt == 2){
MenuString = "Slot 1";
StartCoroutine (LoadCo());
}
break;
case 1:
if (PauseMenuInt == 0){
MenuString = "Save";
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = 1;
}
}
if (PauseMenuInt == 1){
MenuString = "Slot 2";
StartCoroutine (SaveCo());
}
if (PauseMenuInt == 2){
MenuString = "Slot 2";
StartCoroutine (LoadCo());
}
break;
case 2:
if (PauseMenuInt == 0){
MenuString = "Load";
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = 2;
}
}
if (PauseMenuInt == 1){
MenuString = "Slot 3";
StartCoroutine (SaveCo());
}
if (PauseMenuInt == 2){
MenuString = "Slot 3";
StartCoroutine (LoadCo());
}
break;
case 3:
if (PauseMenuInt == 0){
MenuString = "Exit";
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
Application.Quit();
}
}
if (PauseMenuInt == 1){
MenuString = "Back";
if (Input.GetButtonDown("attack1") == true){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = 0;
}
}
break;
case 4:
MenuIndex = 0;
break;
}
if (Input.GetButtonDown("Digipad_down") == true){
audio.PlayOneShot(MenuFocus);
MenuIndex += 1;
}
if (Input.GetButtonDown("Digipad_up") == true){
audio.PlayOneShot(MenuFocus);
MenuIndex -= 1;
}
I dont know whats wrong, please help me.
O$$anonymous$$ I just noticed the save coroutine is doing the same thing. I just suck lol.
You have the same if-clause in your switch as you do in the coroutine. The coroutine is run during the same frame as the switch, so the result of the if-clause is the same in both cases (true).
They are the same yes (input.getbuttondown()) but they are nested in different ifs so in theory they should not run at the same time, but obviously they are because ifs dont fire on its own without meeting the conditions, I cant change the input for each function so I don't know what to do, If only I had designed the game to go with mouse and key board ins$$anonymous$$d of gamepad this would have been really easy... but Unity GUI doesn't like Gamepads.
Answer by raycosantana · Oct 09, 2013 at 06:17 PM
Ok I found the solution, instead of using coroutines I made an if statement and simplified the switch This is the IF I made.
if (Input.GetButtonDown("attack1")== true){
if (MenuString == "Slot 1" || MenuString == "Slot 2" || MenuString == "Slot 3"){
audio.PlayOneShot(SelectMenu);
if (PauseMenuInt== "Save_menu"){
_SaveSystem.Save(MenuString);
}
if (PauseMenuInt== "Load_menu"){
_SaveSystem.Load(MenuString);
}
}
if (MenuString == "Save"){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = "Save_menu";
}
if (MenuString == "Load"){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = "Load_menu";
}
if (MenuString == "Back"){
audio.PlayOneShot(SelectMenu);
PauseMenuInt = "Pause";
}
if (MenuString == "Exit"){
audio.PlayOneShot(SelectMenu);
Application.Quit();
}
if (MenuString == "Resume"){
audio.PlayOneShot(SelectMenu);
_GameManager.MenuBool = false;
}
}
It basically has every possible outcome of pressing the "attack" button, in the switch I just deleted every reference to inputs. Thanks all for your time.
Answer by TrickyHandz · Oct 09, 2013 at 03:38 AM
In order for this to work, you will need to have a coroutine that is specifically waiting for Input. In my experience, conditions inside coroutines work best when they are while loops. Personally, I never tried to structure one like what you have. However, I dug up this article I had in my favorites from years ago that might help you out: Coroutines: Waiting for Input
I'm sure that should get you well on your way to accomplishing this.
Thanks for the reply but it didn't work, I even redesigned the whole thing and yet it doesnt work. $$anonymous$$y guess is the problem is not the coroutines (I have used similar coroutines several times and they work) but the switch, somehow it registers the input of when changing the menu mode (Pause$$anonymous$$enuInt) as it was the input for saving or loading.
Your answer
Follow this Question
Related Questions
If \ Else how does the program reads it? 2 Answers
|| inside if statement not working. (noob) 3 Answers
Problem with blinking!!! Argh 0 Answers