Checking whether an object is active in a scene

Learn how to check whether an object is active in the current scene using GameDriver

A common question is how to check whether an object exists in a scene. One simple way to check this is using the WaitForObject command, such as:

api.WaitForObject("//*[@name='StartButton']");

Alternatively, if you want to trigger an event and check whether the "active" property has been set to "true" for a specific object, you can check using the GetObjectFieldValue command, such as in the example below. 

// This comment sets the object active in the scene
bool hiddenCube = api.CallMethod("//*[@name='HiddenCube']", "SetActive", new object[] { true });

// This command checks that the object is active
bool invisCube = api.GetObjectFieldValue<bool>("//*[@name='HiddenCube']/@activeSelf");

// Test the outcome
Assert.IsTrue(invisCube == true, "The invisible cube is still missing");

Note that WaitForObject will fail if the parent or grandparent of the object is inactive, which is the intended behavior in Unity. Object visibility is inherited as a way to simplify the enabling and disabling of complex object trees.