- Home /
Found the answer elsewhere
Create with Code 4.4 For-Loops For Waves: play mode freeze
Hi I am a beginner and started a little while ago using my laptop to learn how to use Unity but today I ran into a problem with this tutorial.
In Create with Code 4.4 For-Loops For Waves After completing video 2, his play mode worked, while mine freezes and my memory use jumps up to about 95%. I suspect my laptop doesn't enough enough grunt to process the loop and instantiate the game objects in random positions.
I will paste the code below (I continued on despite being unable to test) and attach a pic of my specs.
This is the code (please not I changed some variable names to be more intuitive to me:
public class Spawner : MonoBehaviour { public GameObject prefabSlot; public GameObject pUpPrefab;
private float spawnRange = 8.0f;
public int enemyCount;
public int waveNumber = 1;
void Start()
{
SpawnWave(waveNumber);
Instantiate(pUpPrefab, GenerateSpawnPos(), Quaternion.identity);
}
void Update()
{
enemyCount = FindObjectsOfType<Enemy>().Length;
if (enemyCount == 0)
{
waveNumber++;
SpawnWave(waveNumber);
Instantiate(pUpPrefab, GenerateSpawnPos(), Quaternion.identity);
}
}
void SpawnWave(int enemyPopulation)
{
for (int i = 0; i < enemyPopulation; i = i++)
{
Instantiate(prefabSlot, GenerateSpawnPos(), prefabSlot.transform.rotation);
}
}
private Vector3 GenerateSpawnPos()
{
float spawnPosX = Random.Range(-spawnRange, spawnRange);
float spawnPosZ = Random.Range(-spawnRange, spawnRange);
float dropHeight = 0.0f;
Vector3 randomPos = new Vector3(spawnPosX, 0, spawnPosZ);
return randomPos;
}
}
NV$$anonymous$$. A friend just got back to me with the answer:
I had: for (int i = 0; i < enemyPopulation; i = i++)
but I needed either:
for (int i = 0; i < enemyPopulation; i++)
or
for (int i = 0; i < enemyPopulation; i = i+1)
Follow this Question
Related Questions
Play An Audio Clip When An Instantiated Object Collides 1 Answer
There is a generation lag 1 Answer
Random instantiation endlessly? 1 Answer
Please share ideas for obstacle management 0 Answers
Unity Keeps Freezing on Play 5 Answers