Search Unity

Unity editor freezes after a few seconds when playing my project.

Discussion in 'Editor & General Support' started by Meijer, Jan 10, 2019.

  1. Meijer

    Meijer

    Joined:
    Jun 11, 2015
    Posts:
    15
    Hello,

    When I was coding my EnemySpawner script, and testing it every now and then, the editor suddenly freezed when being in-game for a few seconds. I did a little testing to see what could be causing this weird behaviour, and I found that it's both time- and place related, because when I moved my character to a completely different place in the scene, it also freezed (once, the rest of the times I tried that, it didn't). And it freezes every time I move across a specific coordinate in my scene. As I am working on the EnemySpawner script, it made sense that that would likely be the problem causing this. As I'm just a beginner with both Unity and C#, what could anything in my script possibly cause my editor to crash/freeze? (I am aware that it doesn't work, as well as the fact that it's not exactly "good" programming. Just practicing :)
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class EnemySpawn : MonoBehaviour {
    7.  
    8.     //Variables ------------------
    9.    
    10.     public GameObject Enemy;
    11.     public Transform enemyPosition;
    12.  
    13.     [SerializeField]
    14.     private float repeatRate;
    15.     [SerializeField]
    16.     private float destroyRate;
    17.  
    18.     [SerializeField]
    19.     private int totalEnemies;
    20.     [SerializeField]
    21.     private int EnemiesAllowed;
    22.  
    23.     //----------------------------
    24.  
    25.     private void Start()
    26.     {
    27.         totalEnemies = 0;
    28.     }
    29.  
    30.     private void OnTriggerEnter(Collider other)
    31.     {
    32.         if(other.gameObject.tag == "Player")
    33.         {
    34.             InvokeRepeating("EnemySpawner", 0.5f, repeatRate);
    35.             Destroy(gameObject, destroyRate);
    36.             gameObject.GetComponent<BoxCollider>().enabled = false;
    37.         }
    38.     }
    39.  
    40.     //This is supposed to limit how many enemies can spawn, otherwise the scene would get too cluttered with enemies and a million different "Heartpulse" SFX's would play. But it doesn't work for the time being.
    41.     void EnemySpawner()
    42.     {
    43.         while (true)
    44.         {
    45.             if (totalEnemies <= EnemiesAllowed)
    46.             {
    47.                 Instantiate(Enemy, enemyPosition.position, enemyPosition.rotation);
    48.                 totalEnemies++;
    49.             }
    50.         }
    51.     }
    52. }
     
  2. Meijer

    Meijer

    Joined:
    Jun 11, 2015
    Posts:
    15
    Oh, btw yes I am sure that the enemySpawners are the problem, because when I disable them, it doesn't occur. Just noticed that when I disable the script, it still happens. So I suppose it's somehow related to an empty GameObject with merely a boxCollider (and the script) attached?! Until recently, this never happened though, while they always had a BoxCollider attached.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Code (CSharp):
    1.  if(other.gameObject.tag == "Player")
    2.         {
    3.             InvokeRepeating("EnemySpawner", 0.5f, repeatRate);
    4.             Destroy(gameObject, destroyRate);
    5.             gameObject.GetComponent<BoxCollider>().enabled = false;
    6.         }
    This probably gets called multiple times. Causing this:
    Code (CSharp):
    1. while (true)
    2.         {
    3.             if (totalEnemies <= EnemiesAllowed)
    4.             {
    5.                 Instantiate(Enemy, enemyPosition.position, enemyPosition.rotation);
    6.                 totalEnemies++;
    7.             }
    8.         }
    To happen. This quite the reason for the freeze to happen.
    Use coroutines so you could insert yields between spawns, and prevent your spawner to be spawned multiple times as well.

    Also, this belongs rather to the scripting sub-forum.
     
    Meijer likes this.