Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xeon13 · Apr 20, 2016 at 03:53 AM · c#inputcoroutineswait

Reading Instance Variables Inside a Coroutine

I'm trying to write code that will wait for the user to select an option from a pop-up box before proceeding. The computer will ask the player if it is alright to build something, and wait for the player to select yes or no. Here is some of the code:

 private bool noResponse = true;
 private bool canBuild = true;
 
 void Update () {
      if (!gettingNextAction) {
         gettingNextAction = true;
 
         StartCoroutine(getNextAction());
     }
 }
 
 private IEnumerator getNextAction() {
     yield return StartCoroutine(promptPlayer());
 
     if (canBuild) {
         // Build the building
     }
 
     gettingNextAction = false;
 }
 
 private IEnumerator promptPlayer() {
     buildRequestPanel.SetActive(true);
 
     while (this.noResponse) {
         Debug.Log(this.noResponse);    // This always prints true
         yield return null;
     }

     Debug.Log("Got here");    // This never prints
 
     yield return new WaitForEndOfFrame();
 }
 
 public void playerPromptResponse(bool response) {    // Function called by button press
     if (!response) {
         canBuild = false;
     }
 
     this.noResponse = false;
 
     Debug.Log("In response: " + this.noResponse);    // This prints out false
 }

What I am trying to do is use the while loop in the prompt player method to wait until noResponse is false. noResponse will become false after the playerPromptResponse method is called by the button press in the UI.

This all seems pretty straight forward to me, but when running it and printing out the values, even though noReponse changes to false in the playerPromptResponse method, it is always true in the coroutine.

So is this something that I don't understand about coroutines? Any suggestions about how to get around this? I also tried passing an instance of the class into the coroutine, but that didn't change anything.

Thanks in advance.

Comment
Add comment · Show 2
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 Johat · Apr 20, 2016 at 06:58 AM 0
Share

On the face of it, this definitely looks like it should work -- unless I'm misreading something.

Also, just to be sure, gettingNextAction is a private, non-static bool that defaults to false, right?

One thing I would double check is that it's definitely this instance that the button is calling playerPromptResponse on.

Is your class just a standard $$anonymous$$onoBehaviour?

What print outs do you get? When you start the promptPlayer coroutine, do you get the print out every frame until a prompt is given (maybe add some extra text to make sure it's not getting muddled with another print out of "true")?

Also, is there anything else in this script that you haven't posted? What about the buildRequestPanel -- are there any scripts attached to that GameObject or its active children that do something in OnEnable or equivalent? Anything that may somehow touch this script?

(As a simple test to show that what you're doing should work, create a new scene. Here, create a new script with a public bool and a Coroutine that is started in Start and loops forever printing "The bool is " + the value. $$anonymous$$ake sure it's attached to something. Then toggle the value in the inspector whilst playing. You should see the value change accordingly.)

avatar image user_2015 · Apr 20, 2016 at 07:22 AM 0
Share

Have you tried to access this.noResponse via a Getter?

 bool waitingForResponse(){
     return noResponse;
 }
 
 //Inside your Coroutine
 while(this.waitingForResponse()){
     ...
 }

$$anonymous$$y other idea would be to use a static class field, since there won't be multiple popups at once, right?

 static bool noResponse = false;
 
 //Inside your Coroutine
 while($$anonymous$$yBehaviour.noResponse){
     ...
 }

Please let me know if it works.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Immanuel-Scholz · Apr 20, 2016 at 07:19 AM

I highly suspect that you have two instances of this script and accidentally call the playerPromptResponse function on the wrong instance.

To verify this, you can use the function "GetInstanceID" that is present on every UnityEngine.Object subclass. Change the log statements to include this, for example:

 Debug.Log("In response: " + this.noResponse + " from " + this.GetInstanceID());

I bet you will get different ID's on the response and the promptPlayer. Am I right?

Comment
Add comment · Show 2 · 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 user_2015 · Apr 20, 2016 at 07:31 AM 0
Share

Tip: You can view the instance ID of your script in the inspector by enabling the debug mode. s. screenshot

avatar image xeon13 · Apr 21, 2016 at 04:07 PM 0
Share

It does indeed give me two different instances. I'm assu$$anonymous$$g this is because I attach a prefab of the object with this script to the buttons in the inspector to trigger player prompt response, but when I actually instantiate the object part way through the game it is a different instance?

avatar image
0

Answer by xeon13 · Apr 21, 2016 at 05:18 PM

Changing the variable to static as mentioned in the comment above works as expected. It seems like the issues is because of the two different instances, so in a situation where the object with this script attached to it isn't created dynamically that can likely be avoided easier.

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

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Help with waiting for input? I'm too used to how Console.ReadLine() works. 0 Answers

Getting the text from UIInput 1 Answer

Projection to mouse position in isometric game 1 Answer


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