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 IHackedDeath · Jun 17, 2014 at 12:47 PM · loopingtower-defensepausing

Why is my game pausing?

Hi guys as my above question says why is my game pausing?

I am pretty sure it is an infinity loop in my script but I would think that I wouldn't be able to play it straight away after pressing the pause button, I thought it would have stayed pause until shutting the game down.

Here is my script which I think has the loop in it:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class list : MonoBehaviour {
     public Rigidbody bulprefab;
     public List<Transform> targets;
     public Transform selEnemy;
     private Transform myTransform;
     public bool areEnemies = false;
     public float shotdly = 1.5f;
     private float shotcnt = 0.0f;
     public GameObject gun;
 
     void Update () 
     {
         targets = new List<Transform>();   
         selEnemy = null;
         myTransform = transform;
         AddAllEnemies();
         MoveShoot();
     }
     
     public void AddAllEnemies() {
         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
         
         foreach (GameObject enemy in go) {
             AddTarget (enemy.transform);
         }
     }
 
     public void AddTarget(Transform enemy) {
         targets.Add(enemy);
     }
 
     private void SortTargetsByDistance() {
         targets.Sort(delegate(Transform t1, Transform t2) { 
             return (Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position)));
         });
     }
 
     void MoveShoot(){
         timecount ();
         if(this.areEnemies){
             SortTargetsByDistance();
             selEnemy = targets[0];
             gun.transform.LookAt (selEnemy);
             if (shotcnt >= shotdly){
                 Rigidbody bul;
                 bul = Instantiate(bulprefab, gun.transform.position, gun.transform.rotation) as Rigidbody;
                 bul.velocity = gun.transform.TransformDirection(Vector3.forward * 10);
                 shotcnt = 0.0f;
             }
         }    
     }
 
     void timecount () {
         shotcnt += Time.deltaTime;
     }
 
 
     void OnTriggerStay(Collider enemy){
         if (enemy.gameObject.tag == "Enemy") {
                         areEnemies = true;
                 } else {
                         areEnemies = false;
                 }
     }
 }

Sorry that it is a long script.

My game does say that it doesn't like the selenemy = targets[0]; line, My enemies have the tag Enemy and their name is enemy, i have a second enemy 2 with the tag Enemy as well but it isn't used in the same scene?

Any help is appreciated

Comment
Add comment · Show 5
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 tanoshimi · Jun 17, 2014 at 12:56 PM 1
Share

I'm not quite sure what you mean by "pausing" - you mean the game is running slow/jerking? Currently, in Update() you're re-initialising your targets list, calling FindGameObjectsWithTag to populate it, and then sorting the enemies by distance - this is relatively expensive - do you need to do it every frame? How many enemies do you have in the list?

avatar image Nerevar · Jun 17, 2014 at 01:02 PM 0
Share

You don't have any error messages ?

avatar image haico1992 · Jun 17, 2014 at 01:39 PM 0
Share

Or you press debug in $$anonymous$$onodevelop?

avatar image IHackedDeath · Jun 18, 2014 at 12:38 AM 0
Share

@tanoshimi - I am making a tower defense and with the list, since my enemies are constantly being spawned and killed i would like to always be updating and find out as soon as something happens. And by pausing i mean when i play/test (in unity) my game it all runs smoothly except for when i kill an enemy, as soon as one dies the game pauses or i should say unity pauses the game itself.

avatar image AndyMartin458 · Jun 18, 2014 at 12:54 AM 1
Share

your $$anonymous$$oveShoot function assumes incorrectly that there is a target in the list. You should make sure that AddAllEnemies is correctly finding the targets. Also, you should not be running AddAllEnmies at all times.

  public void AddAllEnemies() {
 GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
  
 Debug.Log("Count of enemies is " + go.count);
 foreach (GameObject enemy in go) {
 AddTarget (enemy.transform);
 }}


 

2 Replies

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

Answer by Rhithik · Jun 18, 2014 at 12:57 AM

If you are getting an error and Unity is pausing, you probably have Error Pause selected.

http://docs.unity3d.com/Manual/Console.html

Comment
Add comment · Show 1 · 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 IHackedDeath · Jun 18, 2014 at 02:00 AM 0
Share

Thank you so much I have never had this selected or even needed to select it so maybe when i was going in between mac and pc something happened or i accidentally pressed it, either way that was the problem.

I feel so annoyed that it was something so simple as this

avatar image
1
Wiki

Answer by Kiwasi · Jun 18, 2014 at 01:07 AM

I don't see an infinite loop here, just some really long and slow ones. Some general thoughts on code efficiency.

Problems

  • Recreating the list of all enemies every frame.

  • Sorting the list every frame

  • Multiple versions on the list (just speculation but it looks like you have a copy of this script on every tower)

To solve

  • Have the enemies check in as they are spawned and check out as they are destroyed. The list will only need to be updated twice per enemy.

  • Only sort the list on frames when you actually need to fire. In other words move the call to SortTargetsByDistance() to inside the if (shotcnt >= shotdly) loop

  • Use a static list. That way all your towers are referencing the same list. No need to recreate it for each tower.

Comment
Add comment · Show 1 · 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 IHackedDeath · Jun 18, 2014 at 02:02 AM 0
Share

Thank you so much, even though the question was answered, all of these points have been taken into account and i have been meaning to change it for a while but thank you for showing me how :)

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

9 People are following this question.

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

Related Questions

audio synthesizer in unity? 1 Answer

Animation Keeps Looping 2 Answers

Unity is cutting a SUBSTANTIAL amount off the end of my MP3 files. 0 Answers

Transforming position for different colliders for repeating background? 0 Answers

Need some help with Array searching logic 1 Answer


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