- Home /
In my game something happens that freezes everything and I know what causes it but I am unsure how to fix it
Basically whenever I press start the game freezes and I know the trigger sort of. I know it is in one of these 4 scripts because I deactivated the thing that triggers them and there is no freeze. These four scripts spawn in the characters. The first one spawns in the player. The second one spawns in the runner zombie. The third one spawns in the normal zombie and the fourth one spawns in the behemoth zombie. I looked through the code and haven't found anything but that is because I am a mega noob at coding.
Player Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawn_Player : MonoBehaviour
{
public GameObject Player;
public float xPos;
public float zPos;
// Start is called before the first frame update
void Start()
{
int x = Random.Range(1, 3);
if (x == 1)
{
xPos = 0.5f;
zPos = Random.Range(1, 215);
}
else
{
xPos = Random.Range(1, 165);
zPos = 0.5f;
}
Instantiate(Player, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
}
}
Runner Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RunnerRandSpawn : MonoBehaviour
{
public GameObject ZK;
public int xPos;
public int zPos;
public int enemyCount;
// Start is called before the first frame update
void Start()
{
while (enemyCount < 25)
{
xPos = Random.Range(1, 165);
zPos = Random.Range(1, 215);
Instantiate(ZK, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
}
Regular Zombie Spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieRandomSpawn : MonoBehaviour
{
public GameObject ZK;
public int xPos;
public int zPos;
public int enemyCount;
// Start is called before the first frame update
void Start()
{
while (enemyCount < 90)
{
xPos = Random.Range(1, 165);
zPos = Random.Range(1, 215);
Instantiate(ZK, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
}
Behemoth Zombie spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BehemothRandSpawn : MonoBehaviour
{
public GameObject ZK;
public int xPos;
public int zPos;
public int enemyCount;
// Start is called before the first frame update
void Start()
{
while (enemyCount < 10)
{
xPos = Random.Range(1, 165);
zPos = Random.Range(1, 215);
Instantiate(ZK, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
}
Answer by Llama_w_2Ls · Apr 28, 2021 at 08:40 PM
Since enemyCount
is a public variable, its value could be modified by other scripts, causing the while loop in your start methods to run indefinitely.
To prevent any unwanted infinite loops, use for
loops instead of while
. For example:
void Start()
{
for (int i = 0; i < 10; i++)
{
xPos = Random.Range(1, 165);
zPos = Random.Range(1, 215);
Instantiate(ZK, new Vector3(xPos, 0.3f, zPos), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613033007im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Which mistake could lead to the detection of a non-existing GameObject? 1 Answer
grab script not working 0 Answers
A Proper Way To Pause A Game 9 Answers