Hi,
I’m trying to define a function that will check if a certain set of objects is moving. The problem is that Unity will crash on me during the execution of this code. Not sure if I’m doing something wrong in the code or perhaps there is an easier method than what I’m trying to use. The function is called when a certain type of object is destroyed if that makes a difference
bool partsAreMoving = true;
while(partsAreMoving)
{
Debug.Log("Loop Start");
partsAreMoving = false;
GameObject[] machineParts = GameObject.FindGameObjectsWithTag("MachinePart");
foreach(GameObject part in machineParts)
{
if(part.rigidbody == null)
continue;
if(part.rigidbody.velocity.magnitude > 0) //i.e. moving
{
Debug.Log("Moving " + part.rigidbody.velocity.magnitude);
partsAreMoving = true; //Crashes if I don't comment this out
}
else
{
Debug.Log("Not moving");
}
}
Debug.Log("Loop end");
}
This will crash Unity when it hits it (I won’t even get the “Loop Start” in the log) but if I comment out the line that sets partsAreMoving to true in the foreach loop it will work fine. What am I doing wrong? Is this too intense a calculation?
Thanks in advance