- Home /
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.
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.)
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.
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?
Tip: You can view the instance ID of your script in the inspector by enabling the debug mode. s. screenshot
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?
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.
Your answer
Follow this Question
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