- Home /
Why are children objects not defined as "inactive" when they technically are by the parent?
I'm having an issue with coroutines attempting to run on inactive game objects.
The coroutine is on a series of child objects under different parents that separate them into groups, so when I want to switch a group off, I set the parent to inactive.
However, my custom event still triggers the code and the coroutine still attempts to run on "inactive" objects. Unity stops it of course, but I'm trying to stop the error from spamming. So I'm checking the activeSelf
flag on the child object to verify if it is active. If not then bypass the remainder of the logic. However this isn't the case since I'm only setting the active flag on the parent object. When I query for activeSelf
the parent returns false
, and the child returns true
.
Shouldn't everything under the parent be false
?
Why is Unity saying that this object isn't active when attempting the run the coroutine when it's parent is inactive, but the flag on the child object says otherwise?
What flag is the Unity system checking to generate this error? I could just traverse up the hierarchy until I find a false
activeSelf
, but why should I do it that way? The flag that Unity is checking to generate the error in the first place is the flag I as the developer should be checking apparently, not activeSelf
.
Coroutine 'SymbioticUpdate' couldn't be started because the the game object 'Spawnling' is inactive!
Answer by Bunny83 · Jan 17, 2020 at 01:05 AM
Your "workaround" is completely unnecessary. Unity provides the property activeInHierarchy which does exactly what you are looking for. activeSelf just returns the active state of the object itself. activeInHierarchy does tell if if the object is effectively active or not.
Answer by ghostravenstorm · Jan 15, 2020 at 09:36 PM
Work around:
/// Returns true if the highest-most parent is active.
/// Returns false at the first instance of an inactive object.
public static bool CheckForActiveParent (Transform t) {
if (t.gameObject.activeSelf) {
if (t.parent != null) return CheckForActiveParent(t.parent);
else return true;
} else return false;
}
Your answer
Follow this Question
Related Questions
Rotate GameObject using Coroutine 2 Answers
Flawed code? 0 Answers
Unity 5 And Attaching Game Objects To Inactive Objects 1 Answer
How to Instantiate single Prefabs from an array? 0 Answers
How can I track if GameObject is null in function? 2 Answers