- Home /
GetComponents.() known to cause crash?
I'm curious about the following line of code, I've had some users report their game crashing inconsistently. The output_log shows the game crashes around the following snipped of code (though I can't quite yet be 100% sure).
My question is, is Unity known for having issues with gameObject.GetComponents.()? This code is reused on all items that can be picked up in game. So depending on the item, it could be built using a single box collider, or a single mesh collider or multiple box colliders. Could this cause the game to crash? Has anyone had issue with this before?
Many thanks.
Written in Unity's Javascript.
var myColliders: BoxCollider[] = gameObject.GetComponents.<BoxCollider>();
for (var bc : BoxCollider in myColliders)
{
bc.isTrigger = true;
}
var myMeshColliders: MeshCollider[] = gameObject.GetComponents.<MeshCollider>();
for (var bc2 : MeshCollider in myMeshColliders)
{
bc2.isTrigger = true;
}
Never had problems with that function here.
Not sure where this script runs but might be worth making sure of if(gameObject)
before calling GetComponents().
I think that's my next step, making sure at least one box collider, or mesh collider exists on the object before calling these. Will update if it seems to solve the issue.
That's not exactly what I'm talking about. That case should simply return a empty (but existing) array, which should not crash the code you posted.
However, if the gameObject
has been previously deleted, then the call to getComponents would crash your code with a "null reference exception". So, I'm suggesting you check the object that contains/calls the particular function that is giving you trouble.