Unity hangs when compiling this.

private void Timer()
{
float countTime = 10f;
float timeLeft = 0f;
Vector2 speed = new Vector2(150,150);

		while (myRigid.velocity.x < speed.x && myRigid.velocity.y < speed.y)
		{
				
				countTime -= Time.deltaTime;
		}




		if (countTime <= timeLeft)
		{
			timeUp = true;
		}
		Debug.Log (countTime);
	}

Could anyone tell me how to edit this code so that Unity doesn’t freeze?
It’s basically a timer that is started whenever an object is below a speed threshold.

As a side question, I’d be happy if you could also tell me of a way to compare rigidbody2D.velocity to a Vector2 (x,y);

Your while loop will never terminate as myRigid.velocity and speed don’t change between each loop, therefore once it starts you have an infinite loop. Just use an “if” statement in the Update or FixedUpdate function if you want to count the time the object is below a certain speed.

Also, you are only checking the speed in a single direction. You could be travelling fast in -X direction and still trigger your speed check. I’ll assume you want to check speed (directionless) versus velocity (directed), in which case use something like this:

const float lowSpeed = 200;
float timeLeft = 0;
bool timeUp = true;

void StartTiming() {
    timeLeft = 10f;
    timeUp = false;
}

void FixedUpdate() {
    if (myRigid.velocity.magnitude < lowSpeed) {
        timeLeft -= Time.deltaTime;
    }

    if (timeLeft <= 0) {
        timeUp = true;
    }
}