- Home /
Question by
itstimetomakelol · Jul 25, 2020 at 04:31 PM ·
scripting probleminstantiateoptimizationbeginnerspawning problems
Instantiate not working?
I want to make a small easy game where you have to shoot the enemies but not shoot the innocents. I thought of them having to spawn in chunks(200 innocents in a 80x80 area) but it doesn't instantiate all prefabs. What did I do wrong in my code and is there a better way to spawn objects like in Dani's balls? (Warning: Code might be kind of messy)
[SerializeField] GameObject innocent;
[SerializeField] GameObject enemy;
[SerializeField] GameObject player;
[SerializeField] GameObject testsquare;
[SerializeField] Transform[] Chunks;
Vector2 moveDirection = new Vector2(0, 0);
private bool[] ChunkB = new bool[8];
public float respawnTime;
public int numOfInnocent = 200;
public int numOfEnemy = 0;
int x = 1;
int y = 1;
void Start()
{
ChunkB[1] = true;
StartCoroutine(InnocentSpawning());
do
{
Instantiate(innocent, new Vector2(Random.Range(-40, 40), Random.Range(-40, 40)), Quaternion.identity);
x++;
} while (x <= numOfInnocent);
do
{
Instantiate(enemy, new Vector2(Random.Range(-40, 40), Random.Range(-40, 40)), Quaternion.identity);
y++;
} while (y <= numOfEnemy);
}
void Update()
{
CheckNextSpawnableChunk();
}
void SpawnInnocent()
{
}
IEnumerator InnocentSpawning()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
SpawnInnocent();
}
}
void CheckNextSpawnableChunk()
{
if (Vector2.Distance(moveDirection, Chunks[1].position) > 60f && ChunkB[1] == true)
{
do
{
Instantiate(innocent, new Vector2(Random.Range(-40, 40) - Chunks[1].position.x, Random.Range(-40, 40) - Chunks[1].position.y)
, Quaternion.identity);
x++;
} while (x <= numOfInnocent);
ChunkB[1] = false;
}
}
Comment