- Home /
I have this problem when I restart my game in Unity, bunch of gameObject spawns at once
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);
}
}
}
hard to say just from that.. was wondering if you are creating a new MeteorSpawn without destroying the old one..
My meteor spawns are fixed, only that I am instantiating random meteor variants from that
Answer by Neowb · Jul 04, 2021 at 07:41 AM
Found out that the problem was with Time.time
for everytime I restart the game, the timer starts the time where it last left off. Fixed it by initializing a public float timer;
then changing from Time.time
to time += time.deltaTime;
.
Your answer
Follow this Question
Related Questions
Unity2D quads not appearing in front of other quads in Android build 1 Answer
Unity Remote 5 android doesnt work.,Unity Remot 5 android doesnt work 2 Answers
scaling error for objects in android 1 Answer
Advertisement.IsReady return false on mobile phone 0 Answers
After i added proguard codes, I cannot connect to Google Play Service 0 Answers