I created this game specifically for android, but when I restart the game or when the player dies, and if I try to restart the game bunch of asteroids spawns from my spawner at once, causing the game to get laggy. Well I created a gameObject that spawns meteors 5 seconds after the previous spawn, the meteor has 4 variants and they each spawns randomly in the spawner, anyone know how to fix this? Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeteorSpawn : MonoBehaviour
{
public Transform[] meteors;
public Transform spawn;
public int i = 0;
void Start()
{
}
void Update()
{
if(Time.time> i){
i += 5;
int spawnRandom= Random.Range(0, meteors.Length);
Instantiate(meteors[spawnRandom], spawn.position, Quaternion.identity);
}
}
}
↧