help with spawning objects
In my object spawning script i spawn objects after 7 seconds but when these 7 seconds were over infinite objects start spawning please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomSpawner : MonoBehaviour
{
float timer = 0.0f;
public int seconds;
public int numberToSpawn;
public int expectation;
public List<GameObject> spawnPool;
public GameObject quad;
private void Start()
{
expectation = 7;
}
public void spawnObjects()
{
int randomItem = 0;
GameObject toSpawn;
MeshCollider c = quad.GetComponent<MeshCollider>();
float screenX, screenY;
Vector2 pos;
for (int i = 0; i < numberToSpawn; i++)
{
randomItem = Random.Range(0, spawnPool.Count);
toSpawn = spawnPool[randomItem];
screenX = Random.Range(c.bounds.min.x, c.bounds.max.x);
screenY = Random.Range(c.bounds.min.y, c.bounds.max.y);
pos = new Vector2(screenX, screenY);
Instantiate(toSpawn, pos, toSpawn.transform.rotation);
}
}
private void Update()
{
timer += Time.deltaTime;
seconds = (int)(timer % 60);
if (Input.GetKeyDown(KeyCode.Space))
{
spawnObjects();
}
if (seconds == expectation)
{
expectation =+ 7;
Invoke("spawnObjects", 0.2f);
}
}
}
Comment
Your answer

Follow this Question
Related Questions
Client Spawning Player Objects 0 Answers
Spawning issues 1 Answer
my objects is spawning on the wall 1 Answer
Random spawn script 1 Answer
make enemy fire at randomly times? 0 Answers