Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by raycosantana · Oct 09, 2013 at 01:43 AM · coroutineif

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raycosantana · Oct 09, 2013 at 02:22 AM 0
Share

O$$anonymous$$ I just noticed the save coroutine is doing the same thing. I just suck lol.

avatar image Jamora · Oct 09, 2013 at 04:42 PM 0
Share

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).

avatar image raycosantana · Oct 09, 2013 at 05:08 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raycosantana · Oct 09, 2013 at 04:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to make an if statement with two conditions? 1 Answer

Coroutine not working properly on Android (C#) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges