- Home /
Variable value not updating in test script
Hi, While running some tests I am stuck at something I do not understand. There is a Boolean variable in my test script whose value I get from a script attached to a game object inside the scene that I am loading at the start of the test. The code is below :
PrepareScene();
yield return null;
//yield return new WaitForSeconds(10f);
Testbool = GameObject.Find("TestSuite").GetComponent<TestSuite>().hasLoaded;
while (Testbool == false)
{
yield return null;
}
In the above piece of code I am assigning the value to the Test bool. However the value of test bool remains to be false forever and the test gives me a timeout error after 30 secs even though I am sure that hasLoaded becomes true at some point. If I use waitforseconds(10) and then assign the value of Testbool it becomes true. But the value of Testbool does not update once it has been assigned
Answer by karl_jones · Sep 03, 2019 at 02:45 PM
You are getting the TestBool, by value, not reference. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/value-types
Testbool = GameObject.Find("TestSuite").GetComponent<TestSuite>().hasLoaded;
This is copying the value into Testbool. If the value of hasLoaded changes it will not be reflected back to TestBool. You should instead hold a reference to the GameObject and check hasLoaded directly.
var suite = GameObject.Find("TestSuite").GetComponent<TestSuite>();
while (suite.hasLoaded == false)
{
yield return null;
}
Hi, thank you I have solved this issue. I have one more question though, my test script is derived from TestSuite and TestSuite has a bool named hasloaded.
I cannot just use this in my test script?
while(hasloaded == false) { yield return null; }