Question by
hal0000 · May 16, 2018 at 08:40 PM ·
scene-loadingtime.deltatimescene-changerestarttimer countdown
Somethings Wrong After Restarting Scene
Hi,
I am making a Tower Defense Game and after pressing Restart Button, sometimes restarting well, sometimes not restarting properly i need help.
This is my restart code:
public void Toggle()
{
ui.SetActive(!ui.activeSelf);
topUI.SetActive(!topUI.activeSelf);
if (ui.activeSelf)
{
Time.timeScale = 0f;
}
else
{
Time.timeScale = 1f;
}
}
public void retry()
{
scenefader.FadeTo((SceneManager.GetActiveScene().name));
Toggle();
}
and i believe this is corrupt script:
public float timeBetweenWaves = 5f;
public float countdown = 5f;
private int waveIndex = 0;
public Text waveCountDownText;
void Update()
{
if(EnemiesAlive > 0)
{
return;
}
if (countdown <= 0f && EnemiesAlive<=0)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountDownText.text =Mathf.Round(countdown).ToString();
waveCountDownText.text = string.Format(" {0:00.00}", countdown);
}
IEnumerator SpawnWave()
{
PlayerStats.Rounds++;
WaveInfo wave = waves[waveIndex];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
waveIndex++;
if (waveIndex == waves.Length)
{
Debug.Log("level won");
this.enabled = false;
}
}
You can see on top middle of screen time countdowning and when it's equal to 0 wave starting but sometimes after restarting the scene i don't know why time is not starting to count down and not showing it on text too. You can see on Second Picture time is not showing and wave is won't starting.
adsızzzzzzzzzzzzzzzzzz.png
(259.3 kB)
adsızz2222222222222222222zz.png
(242.3 kB)
Comment
Best Answer
Answer by hal0000 · May 16, 2018 at 10:11 PM
i fixed by debugging i changed my code to this :
void Start()
{
countdown = 5f;
waveCountDownText.text = Mathf.Round(countdown).ToString();
EnemiesAlive = 0;
}
void Update()
{
if (EnemiesAlive > 0)
{
return;
}
else
{
if (countdown <= 0f)
{
StartCoroutine(SpawnWave());
countdown = timeBetweenWaves;
return;
}
else
{
countdown -= Time.deltaTime;
countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
waveCountDownText.text = Mathf.Round(countdown).ToString();
//waveCountDownText.text = string.Format(" {0:00.00}", countdown);
}
}
}
IEnumerator SpawnWave()
{
PlayerStats.Rounds++;
WaveInfo wave = waves[waveIndex];
for (int i = 0; i < wave.count; i++)
{
SpawnEnemy(wave.enemy);
yield return new WaitForSeconds(1f / wave.rate);
}
waveIndex++;
if (waveIndex == waves.Length)
{
Debug.Log("level won");
this.enabled = false;
}
}
Your answer
Follow this Question
Related Questions
Why isn't my start screen loading? 0 Answers
Build errors in unity 0 Answers
Restarted scene not working properly 0 Answers