[SOLVED]The script don't execute after scene reloading
I have this script which don't execute after reloading scene from a button but execute when doing the same thing from another button with the same line of code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class WaveSpawner : MonoBehaviour {
public static int EnemiesAlive = 0;
public Wave[] waves;
public Transform[] spawnPoints;
public float timeBetweenWaves = 5f;
public float countdown = 2f;
//public float timeBetweenEnemies = 0.5f;
public Text waveCountdownText;
public Text wavesText;
private int waveIndex = 0;
private void Awake()
{
this.enabled = true;
}
private void Start()
{
Debug.Log(waveIndex + " " + waves.Length + " " + this.enabled);
if (spawnPoints.Length == 0)
{
Debug.LogError("No spawn points referenced!");
}
}
private void Update()
{
if(EnemiesAlive > 0)
{
return;
}
if (waveIndex == waves.Length)
{
Debug.Log("LEVEL WON!");
this.enabled = false;
}
if (countdown <= 0f)
{
if(waveIndex == waves.Length)
{
return;
}
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountdownText.text = string.Format("{0:00.00}", countdown);
wavesText.text = (waveIndex + 1) + "/" + waves.Length;
}
//IEnumerator SpawnWave()
//{
// //Debug.Log("Wave Incoming");
// Wave wave = waves[waveIndex];
// for (int i = 0; i < wave.count; i++)
// {
// SpawnEnemy(wave.enemy);
// yield return new WaitForSeconds(1f / wave.rate);
// }
// waveIndex++;
// PlayerStats.Rounds++;
// if(waveIndex == waves.Length)
// {
// Debug.Log("LEVEL WON!");
// this.enabled = false;
// }
//}
IEnumerator SpawnWave()
{
//Debug.Log("Wave Incoming");
Wave wave = waves[waveIndex];
for (int a = 0; a < wave.waveCount; ++a)
{
Transform sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
yield return new WaitForSeconds(1f / wave.waveRate);
for (int i = 0; i < wave.enemyWave.Length; i++)
{
for (int j = 0; j < wave.enemyWave[i].enemyCount; ++j)
{
SpawnEnemy(wave.enemyWave[i].enemy, sp);
yield return new WaitForSeconds(1f / wave.enemyWave[i].enemyRate);
}
}
}
waveIndex++;
PlayerStats.Rounds++;
//if (waveIndex == waves.Length)
//{
// Debug.Log("LEVEL WON!");
// this.enabled = false;
//}
}
void SpawnEnemy(GameObject enemy, Transform _sp)
{
Instantiate(enemy, _sp.position, _sp.rotation);
EnemiesAlive++;
}
//void DestroyEnemyOnTargetReach()
//{
// Destroy(gameObject);
//}
}
Answer by Vollmondum · Apr 11, 2019 at 08:21 AM
Because you should never ever disable/enable any scripts, ever. Add a bool and set it to false when want script not to execute. Moreover why setting this.enabled into Awake. Just leave it enabled. And remove all this.enabled lines
I want the script to be disabled when all the waves have spawned and don't execute. Setting a bool does not help. Still same issue. It work when reloaded from one button and does not when reloaded from another button
you need to replace those lines to check which line disables it. Print something right after the bool is set. When found, switch it back to disabling
right now I disabled the enabled lines I made a bool isDone it is set to true as soon the waveIndex = waves.Lenght. At the restart is set to false and still not work. Is strange this happen only on game over is not happen when I restart the level When restarting EnemiesAlive is not reseting to 0 so I added on Start function EnemiesAlive = 0 now is working like a charm
Your answer
Follow this Question
Related Questions
2D Platformer Gun Equip Script Not Working 0 Answers
Continuously monitor childrens properties 1 Answer
problem with asset 2 Answers
I seriously cannot learn how to script. What do I do? 4 Answers
Determine an unknown script 0 Answers