- Home /
problem changing spawnTime variable
I'm having trouble getting my objects to react accordingly. For testing, I have my timer set to spawn every second, then, when my score reaches 5 or above, I would like to change the timer to every 5 seconds.
As it is, the enemies still come every second. I have a Debug.Log() in there and it displays the spawnTime variable as 5, but the enemies still come every second.
I have a feeling it's because it is in my Start() function but in the update function, hundreds of enemies spawn. How can I get around this? Thanks
using UnityEngine;
using System.Collections;
public class spawn : MonoBehaviour
{
// Prefab to spawn
public GameObject spawnableObject;
public GameObject[] prefab;
private int timeToBegin = 2;
private float spawnTime = 1;
Vector3[] spawnPoints;
int endOfArray;
void Start()
{
//The point in which the new prefab will be spawned
GameObject[] objs = GameObject.FindGameObjectsWithTag("SpawnPoints") as GameObject[] ;
spawnPoints = new Vector3[objs.Length] ;
for(int i = 0 ; i < objs.Length ; i++)
{
spawnPoints[i] = objs[i].transform.position ;
}
endOfArray = spawnPoints.Length;
InvokeRepeating ("Spawn", timeToBegin, spawnTime);
}
void Update()
{
if(accelShotHS.score >= 5)
{
spawnTime = 5.0f;
Debug.Log(spawnTime);
}
}
}
Answer by mediumal · Aug 12, 2014 at 11:26 PM
Right after you set your spawnTime to 5 you also need to cancel the old InvokeRepeating() call using CancelInvoke(), then call InvokeRepeating() again with the new spawnTime.
Thanks for the update, it works kind of in a way that i want to but i'm having another error, i've posted another question if you wish to help again
Your answer
Follow this Question
Related Questions
Spawn object with different material. 0 Answers
Player GameObject changes position when I start the game 4 Answers
Initialising List array for use in a custom Editor 1 Answer
How do I create an effective spawn point script for multiple scenes and change music each scene? 1 Answer
Model change position to (0,0,0) when I Start the game. 1 Answer