- Home /
What part of my While Loop is causing the game to freeze? (C#)
Hey guys, before I ask my question, I will give you guys a quick overview of what I am doing and what I want to achieve. I am working on a Real-Time Strategy game, and currently have scripts to make a GUI with buttons to spawn units, and a GUI box that includes labels telling you what your current gold is, gold per minute, total workers, etc. I am relatively new to Unity3D, and C# scripting in general, but I have basic knowledge of Java, C++, Python, Blitz, and more. When my units spawn, they are instantiated at empty GameObjects that I call Team1WarriorSpawn, Team1ArcherSpawn, and so on. When they spawn, they do nothing but stand at their respective spawn points.
Therefore, I need a script to move the newly instantiated units to the other team's castle. I already have tags set up that inform the units of what team they are on, what team to attack, and things like that. I set up the following While Loop inside a method called AttackEnemyCastle()...
public void AttackEnemyCastle(){
float distanceFromCastle = Vector3.Distance(attackPoint.transform.position, unit.transform.position);
while(distanceFromCastle > 1){
Debug.Log("Moving towards enemy castle.");
//look at target
unit.transform.rotation = Quaternion.Slerp(unit.transform.rotation,Quaternion.LookRotation(attackPoint.position - unit.transform.position),rotationSpeed * Time.deltaTime);
Debug.DrawLine(unit.transform.position, attackPoint.position, Color.yellow);
//move towards target
unit.transform.position += unit.transform.forward * moveSpeed * Time.deltaTime;
}
}
After I hit play with the Script attached to the team's GameSettings (GameObject I use for variables like currentGold, currentWorkers, unitCosts, etc.), the game runs fine, without a hitch. But as soon as I click the button to spawn a warrior, or archer for that matter, the game instantly freezes, and I have to force quit out of Unity. Any help would be appreciated, I hope I was descriptive enough without overdoing anything. Thanks in advance for your answers!
Answer by Eric5h5 · Jul 07, 2012 at 10:20 PM
It's freezing because if distanceFromCastle is greater than 1, it goes into an infinite loop...since you never set distanceFromCastle less than or equal to 1 at any point in the loop, it can never exit the loop in order to do anything else.
Okay, so that is the cause of the freeze - an infinite loop. I knew that the game did crash per se, but rather was just glitching in another area. I am going to replace 1 with unitAttackRange, or something along those lines.
But I am a bit confused on removing this infinite loop. How would I get around this? Shouldn't my unit move towards the enemy castle, then once they are within their attack range, stop moving? I want to call on this script in order to move my unit, set attack orders, and things like that, so should there be seperate methods for each action? Like I said, I am fairly new at both Unity and C#, so more help would be appreciated.
And thank you for your answer! $$anonymous$$uch appreciated.
As I mentioned, you're never setting distanceFromCastle inside the loop anywhere, so unless you do that it will always be an infinite loop. But even if you did that, it would all execute immediately in one frame, so you'd be better off using a coroutine in order to allow time to pass.
Oh my, you're right. I completely forgot to add that in! Thanks Eric, I'm thinking about just redoing the entire script. I've browsed the Forums and UnityAnswers, and I constantly see your name pop up. Thanks for all you do, you are an inspiration to all of the new developers out there, like me.
Answer by Solus · Jul 08, 2012 at 02:56 AM
Eric is right, a coroutine would work for this issue.
Another alternative is to consider taking care of the unit's movement in the unit script's update. In general you want to handle things like movement or other things that happen over time in an update. That's why they exist.
Your answer
Follow this Question
Related Questions
Using for as while. 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
I dont know why im getting a null reference exception 1 Answer
Executing coroutines consecutively 0 Answers