- Home /
Calling property isTrigger from child gameobject crash Unity on play
If you try to access a collider from your child gameobject and use isTrigger property Unity crash on playmode (infinite loading scene).
Collider[] collidersObj = gameObject.GetComponentsInChildren<Collider>();
if (collidersObj == null || collidersObj.Length == 0) { return gameObjectcollider; }
for (int index = 0; index < collidersObj.Length; index++)
{
Collider returnCollider = collidersObj[index];
if (returnCollider != null && !returnCollider.isTrigger)
{
// If some child have a colliders not on trigger mode
return returnCollider;
}
}
Answer by Atto2O · Oct 30, 2021 at 06:21 PM
I think the problem is on "gameObject.GetComponentsInChildren();" use because it can return a characterController and it misses the property "isTrigger", making unity crash.
Collider[] collidersObj = gameObject.GetComponentsInChildren<Collider>();
if (collidersObj == null || collidersObj.Length == 0) { return gameObjectcollider; }
for (int index = 0; index < collidersObj.Length; index++)
{
Collider returnCollider = collidersObj[index];
if (returnCollider != null && !ColliderHelper.isTrigger(returnCollider))
{
// If some child have a colliders not on trigger mode
return returnCollider;
}
}
public class ColliderHelper
{
public static bool isTrigger(Collider colliderObject)
{
if (colliderObject == null)
{
throw new ArgumentException("colliderObject is null");
}
if (colliderObject.GetType() == typeof(SphereCollider))
{
return ((SphereCollider)colliderObject).isTrigger;
}
else if (colliderObject.GetType() == typeof(BoxCollider))
{
return ((BoxCollider)colliderObject).isTrigger;
}
else if (colliderObject.GetType() == typeof(CapsuleCollider))
{
return ((CapsuleCollider)colliderObject).isTrigger;
}
else if (colliderObject.GetType() == typeof(CharacterController))
{
return false;
}
throw new ArgumentException("collider type is does not exist, name:"+ colliderObject.name);
}
}
I think there's more to it than just this. All of those Collider types (and CharacterController) are derived from Collider, which is the level where isTrigger is at, so they're all just using that as-is.
That said, I'm also not really clear, based on this update, how it was originally supposed to work. What GameObject/Collider was your static "ColliderHelper.isTrigger" supposed to reference originally? It seems more likely that that was the only real problem, and that 4 unique GetType() calls is unnecessary/overkill.
Also, though it's old (2013), this provides some additional feedback/insight on CharacterControllers and isTrigger functionality/compatibility.
Sry that "ColliderHelper.isTrigger" was a typo.
I updated it with: "returnCollider.isTrigger".
I made some more tests: when u use "gameObject.GetComponentsInChildren();" it can return a characterController and it misses the property "isTrigger", making unity crash.
Your answer
