Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Z13J · Jul 07, 2012 at 10:07 PM · c#gameloopwhilefreezing

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Z13J · Jul 07, 2012 at 11:51 PM 0
Share

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.

avatar image Eric5h5 · Jul 08, 2012 at 12:21 AM 0
Share

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.

avatar image Z13J · Jul 08, 2012 at 01:08 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges