Wondering if I can use .SetActive to iterate through a GameObject and it’s children to enable/disable that part of all of them.
Otherwise, I’ll stick to GetComponentsInChildren.
If not, this would be a damn cool feature.
Wondering if I can use .SetActive to iterate through a GameObject and it’s children to enable/disable that part of all of them.
Otherwise, I’ll stick to GetComponentsInChildren.
If not, this would be a damn cool feature.
This might help:
using UnityEngine;
public static class EnableHelper
{
public static void EnableChildren(this Component component, System.Func<Component, bool> predicate, bool enabled = true)
{
foreach(var c in component.GetComponentsInChildren<Collider>(true))
if(predicate(c))
c.enabled = enabled;
foreach(var c in component.GetComponentsInChildren<Rigidbody>(true))
if(predicate(c))
c.isKinematic = !enabled;
foreach(var c in component.GetComponentsInChildren<Behaviour>(true))
if(predicate(c))
c.enabled = enabled;
}
public static void EnableChildren(this Component component, bool enabled = true)
{
component.EnableChildren((a)=>true, enabled);
}
public static void EnableChildren(this GameObject go, bool enabled = true)
{
go.transform.EnableChildren((a)=>true, enabled);
}
public static void EnableChildren(this GameObject go, System.Func<Component, bool> predicate, bool enabled = true)
{
go.transform.EnableChildren(predicate, enabled);
}
}
Then you can just do:
gameObject.EnableChildren();
gameObject.EnableChildren(false);
gameObject.EnableChildren(c=>c.CompareTag("Something"));