- Home /
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
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?
@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.
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);
}}
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.
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
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 theif (shotcnt >= shotdly)
loopUse a static list. That way all your towers are referencing the same list. No need to recreate it for each tower.
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 :)