- Home /
Question by
LittleJ · Mar 19, 2015 at 12:18 PM ·
javascriptarrayactive
How do i check if all tagged objects are inactive?
I have multiple objects that are active and when shot they deactivate.
How can i check when all of them are deactivated and then play audio?
Here is what i have tried..
function Update () {
for(var fooObj : GameObject in GameObject.FindGameObjectsWithTag("Cubetarget"))
{
if(fooObj.active == false)
{
audio.Play();
print("cubetargets are no longer active");
}
print("cubetargets are still active");
}
}
This script is attached to the parent of all the targets as they are all grouped. It only says "Cubetargets are still active"
Could anyone tell me where I am going wrong?
Thanks
Comment
Best Answer
Answer by Landern · Mar 19, 2015 at 12:30 PM
Give this a go:
function Update () {
var targets: GameObject[] = GameObject.FindGameObjectsWithTag("Cubetarget"); // Get the array of gameobjects
var areAnyTargetsActive : Boolean = false; // Make an assumption, no active targets
for (var i : int = 0; i < targets.Length; i++ ) {
if (targets[i].active == true) {
areAnyTargetsActive = true;
break; // break out of the for loop early since continuing would waste time
}
}
if (areAnyTargetsActive == true) {
print("cubetargets are still active");
} else {
print("cubetargets are no longer active");
}
}
Answer by fafase · Mar 19, 2015 at 01:08 PM
FindGameObjectsWithTag only returns active objects.
GameObject [] objs = GameObject.FindGameObjectsWithTag("Cubetarget");
if(objs.Length == 0)
{
// No active items
}