I’m not looking for a way to fix the code since this wouldn’t have the intended effect anyway, I was just wondering why it made Unity crash. I already got the code working.
The code was supposed to make this sprite move away from the player, but it just crashed Unity instead.
public Rigidbody2D rb2D;
public Transform player;
public float sensePlayerRadius = 3f;
public float stopRunningRadius = 6f;
public float speed = 10f;
bool isRunning = false;
void Update()
{
StartRunningAway();
}
void StartRunningAway()
{
// If player is within the range the emu can sense
if (Vector3.Distance(transform.position, player.position) <= sensePlayerRadius && !isRunning)
{
print("run");
isRunning = true;
RunAway();
}
else print("safe");
}
void RunAway()
{
while(Vector3.Distance(transform.position, player.position) <= stopRunningRadius)
{
// Moves away from the player, a negative distance to move results in the object moving away from the target, not towards, multiplied by deltaTime to be framerate independent
rb2D.MovePosition(Vector3.MoveTowards(transform.position, player.position, -speed * Time.deltaTime));
}
isRunning = false;
}
I suspect it has something to do with the while loop in RunAway. My primary thought is, the loop isn’t breaking and causing a memory overflow
[edit] Are you certain the rigidbody movement is changing the location of the transform being used for distance? If that transform isn’t being affected by the rigidbody movement, then that’d definitely cause the infinite loop 
In your while loop you have you have a negative speed -speed
. Meaning that you are moving in a negative direction if I am not mistaken. Meaning you never exit the loop.
I would recommend say use the following code instead:
void RunAway () {
if (Vector3.Distance(transform.position, player.position) <= stopRunningRadius)
return;
rb2D.MovePosition(Vector3.MoveTowards(transform.position, player.position, speed * Time.deltaTime));
}
@sean244 Please stop going through every single one of my posts and downvoting it, it is not necessary to behave like that simply because you made a few mistakes that were pointed out.
Well, there are a few things going on here. As others have mentioned, it’s your while() loop that’s getting stuck in an infinite loop.
First things first, why is it stuck?
Update() and FixedUpdate() take place at different times from each other. When you apply updates to the Rigidbody's position in Update(), the new position will not be reflected in Update()-based position data (i.e. the Transform) until the next FixedUpdate() cycle has gone through.
Proof of this:
void Update()
{
if(Input.GetKeyDown(KeyCode.L))
{
// rigidbody.position: Changes immediately because it's actively being affected
// transform.position: Won't change until FixedUpdate() when the rigidbody is processed again
Debug.Log("Before: " + transform.position + " --- " + rb.position); // (0, 0, 0) --- (0, 0, 0)
rb.MovePosition(transform.position + Vector3.up);
Debug.Log("After: " + transform.position + " --- " + rb.position); // (0, 0, 0) --- (0, 1, 0)
}
}
Because Update() gets stuck in a loop, FixedUpdate() never gets its turn and the Transform's position will never see the changes made to the Rigidbody's position.
However, a different question comes to mind:
Are you certain you want to use a while() loop for this?
By using while(), the object will move a potential infinite number of times between frames. If you’re looking for a gradual movement, you only need to use an if() statement under the same terms, since Update() IS your loop.
Edit: Clarification and formatting
it seems you did not use yout isRunning variable, i suggest u use it befire while loop.
void RunAway()
{
if(isRunning){
while(Vector3.Distance(transform.position, player.position) <= stopRunningRadius)
{
// Moves away from the player, a negative distance to move results in the object moving away from the target, not towards, multiplied by deltaTime to be framerate independent
rb2D.MovePosition(Vector3.MoveTowards(transform.position, player.position, -speed * Time.deltaTime));
}
isRunning = false;
}