- Home /
Loading game forever?
I added this:
if (Distance < MeetDistance)
{
FollowerState = FollowOrIdle.Following;
}
if (Distance > SeparateDistance)
{
FollowerState = FollowOrIdle.Idle;
}
while (FollowerState == FollowOrIdle.Following)
{
to make the code say this:
if (Distance < MeetDistance)
{
FollowerState = FollowOrIdle.Following;
}
if (Distance > SeparateDistance)
{
FollowerState = FollowOrIdle.Idle;
}
while (FollowerState == FollowOrIdle.Following)
{
if (Leader.rigidbody2D.transform.position.x > rigidbody2D.transform.position.x + GeneralDistance)
{
rigidbody2D.transform.position += Vector3.right * Speed * Time.deltaTime;
}
if (Leader.rigidbody2D.transform.position.x < rigidbody2D.transform.position.x - GeneralDistance)
{
rigidbody2D.transform.position += Vector3.left * Speed * Time.deltaTime;
}
if (Leader.rigidbody2D.transform.position.y > rigidbody2D.transform.position.y + GeneralDistance)
{
rigidbody2D.transform.position += Vector3.up * Speed * Time.deltaTime;
}
if (Leader.rigidbody2D.transform.position.y < rigidbody2D.transform.position.y - GeneralDistance)
{
rigidbody2D.transform.position += Vector3.down * Speed * Time.deltaTime;
}
}
}
}
and now the game is loading forever. why?
Answer by robertbu · Nov 16, 2014 at 05:29 PM
Proper indentation would help make this readable and the problem more obvious. The issue is that you are do a while loop with this condition:
FollowerState == FollowOrIdle.Following
...but nothing in the loop changes 'FollowerState', and you do not 'yield' in the loop, so control never goes anywhere else that could change 'FollowerState' to something besides 'FollowOrIdle.Following'. So you cycle in the loop forever hanging Unity.
It does, but it's true forever. The state never changes within your code. Please spend some time with the resource I shared in my answer. It will really help you, I promise!
Answer by AlwaysSunny · Nov 16, 2014 at 05:29 PM
Please consider dedicating some time to purposefully learning the C# language. It is your ad-hoc approach to learning that is giving you so many problems.
Please bookmark this great resource for learning C# basics very quickly. It's also a terrific reference: http://rbwhitaker.wikidot.com/c-sharp-tutorials
A while statement will execute until the condition becomes false. Since your while statement block never changes the state, it creates an infinite loop, causing Unity to hang.
Your answer
Follow this Question
Related Questions
Application.Quit not working?! 5 Answers
Compiled Game Crashing 1 Answer
Any idea why collision detection not working? [Video] 0 Answers
Save/Load Game and buttons New Game/Load Game? 1 Answer
Simple Code Not Working 4 Answers