Spawning stops after second scene load
I am attempting to create a sort of tower defense game, where there are a certain number of spawns for each enemy type per level...Level 1 = 10 spawns, Level 2 = 12 spawns, etc. I have been using the following code for a button to restart the level.
// Restart Button
void Restart()
{
// Restart the scene
SceneManager.LoadScene("Level 1");
// Resume the normal game speed
Time.timeScale = 1f;
}
This works as intended, so I thought I would use similar functionality to handle going between levels. The idea behind this being that I'm simply increasing the number of spawns/introducing new enemies based on the current level, while maintaining the same scene. With Debug.Log() I have been able to determine that the CurrentLevel value increases between each level, and that the following function actually runs at Start().
// Function to repeat spawning
void InvokeSpawn()
{
// Invoke the function to repeat based on the Spawn Time
if (currentLevel <= 5)
{
Debug.Log("Enemy1 spawning begun");
// Spawning Enemy 1
InvokeRepeating("Spawn_Enemy1", spawnTime, spawnTime);
}
}
Unfortunately, the InvokeRepeating function never runs after going to the next level.
Here is the code for spawning the first enemy
// Enemy 1 Spawn
void Spawn_Enemy1()
{
// Randomly select the next spawn point
int spawnPointIndex = Random.Range(0, spawnPoints.Length);
// Stop spawning enemies once the counter reaches 0
if (enemy1Spawns >= 0)
{
Debug.Log("Enemy 1 spawned");
// Create the enemy at the randomly selected spawn point
Instantiate(enemy1, spawnPoints[spawnPointIndex].position,
spawnPoints[spawnPointIndex].rotation);
}
// Decrease the number of spawns
enemy1Spawns -= 1;
// Decrease total spawns
totalSpawns -= 1;
}
And the code when the NextLevel button is clicked
// Next Level functionality
public void NextLevel()
{
//Reset the score for the current level
ScoreManager.score = 0;
// Hide the Shop
shopUI.SetActive(false);
// Increment the current level counter by 1
GameManager.currentLevel += 1;
// Resume the game
Time.timeScale = 1f;
// Reload the scene
SceneManager.LoadScene("Level 1", LoadSceneMode.Single);
}
I've tried various solutions like ensuring no values are static, different methods of stopping/starting the spawn using InvokeRepeating/CancelInvoke(), and playing with the number of spawns, but nothing has worked. Everything seems to be working correctly and has the appropriate values up until it gets to InvokeRepeating("Spawn_Enemy1") functionality. The only static variables are the CurrentLevel and Score in order to track them between levels. Should that be done differently? Am I missing something here? Perhaps I'm doing something out of order or need to separate some of the functionality? Any help would be great and is always appreciated :)
Edit: I've also attempted starting the game at a higher level (4 for example), which led to the same issue. The appropriate enemies/number of enemies spawned for level 4, but the subsequent level does not spawn anything.
// Decrease the number of spawns
enemy1Spawns -= 1;
// Decrease total spawns
totalSpawns -= 1;
Do you ever reset the values above?
Answer by Dakotes · Aug 27, 2017 at 05:12 PM
@Genome I do not reset the values. Here is the code where they are declared
// Public
public int totalSpawns;
public int enemy1Spawns = 1;
public int enemy2Spawns = 1;
public int enemy3Spawns = 1;
totalSpawns is calculated on Awake() to get the total number of enemies for that level (enemy1Spawn + enemy2Spawn + enemy3Spawn). The enemy spawns are public so that I can modify them from the inspector, and totalSpawns is public so that it can be accessed from other scripts. I just tested and both the enemy1Spawns and totalSpawns increment correctly when the next level is loaded. I assumed that reloading the scene would reset the values since it is running a new instance of the script. Please correct me if I'm misunderstanding how it all ties together.
Edit: I figured it out. After ensuring all the GameObjects were destroyed before the scene was loaded everything worked perfectly.
So, like I said, an object was persisting between level loads?
Answer by tablekorner · Aug 27, 2017 at 06:00 PM
It sounds like what's happening is that your InvokeSpawn() function is only running one time even through the scene is being reloaded because the Start() function itself is only being called once. Perhaps whatever object you have the script with that Start() function on is persisting between level loads? If that's the case, then Start() will not be called again as it's only called once in a scripts lifetime.
Your answer
Follow this Question
Related Questions
Spawning a number of enemies while condition is true 0 Answers
Soccer game, more realistic net 1 Answer
Problem with destroying a gameobject 2 Answers
Fade Object Out On Scene Change 1 Answer