2D Platform Course. Video 21-22.

Updated on March 7, 2018 in [A] 2D
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on March 4, 2018

Hello everyone and first of all – thanks Brackeys for such amazing videos and tutorials. However, GUI is still unbreakable for me. I had lot of problems here, while everything so far was more or less smoothly.

Brief description of the problem. Counting starts from 4 (Why?), than goes to WAVE Incoming, than again Counting. Why it happening?

I even copied script from ready already documents – didn’t helped. Re-write eveyr animation from scratch – zero results. 
Here is video – https://www.youtube.com/watch?v=vPnv-HJfqcc&feature=youtu.be
Here is Wave UI Script

using UnityEngine.UI;
using UnityEngine;

public class WaveUI : MonoBehaviour {

[SerializeField]
WaveSpawner spawner;

[SerializeField]
Animator waveAnimator;

[SerializeField]
Text waveCountdownText;

[SerializeField]
Text waveCountText;

private WaveSpawner.SpawnState previousState;

// Use this for initialization
void Start () {
if (spawner == null)
{
Debug.LogError(“No Spawner refferenced”);
this.enabled = false;
}
if (waveAnimator == null)
{
Debug.LogError(“No waveAnimator refferenced”);
this.enabled = false;
}
if (waveCountdownText == null)
{
Debug.LogError(“No waveCountdownText refferenced”);
this.enabled = false;
}
if (waveCountText == null)
{
Debug.LogError(“No waveCountText refferenced”);
this.enabled = false;
}
}

// Update is called once per frame
void Update () {
switch (spawner.State)
{
case WaveSpawner.SpawnState.COUNTING:
UpdateCountingUI();
break;
case WaveSpawner.SpawnState.SPAWNING:
UpdateSpawningUI();
break;

}

previousState = spawner.State;
}
void UpdateCountingUI()
{
if (previousState != WaveSpawner.SpawnState.COUNTING)
{
waveAnimator.SetBool(“WaveIncom”, false);
waveAnimator.SetBool(“WaveAnim”, true);
//Debug.Log(“COUNTING”);
}
waveCountdownText.text = ((int) spawner.WaveCountdown).ToString();

}
void UpdateSpawningUI()
{
if (previousState != WaveSpawner.SpawnState.SPAWNING)
{
waveAnimator.SetBool(“WaveAnim”, false);
waveAnimator.SetBool(“WaveIncom”, true);

waveCountText.text = spawner.NextWave.ToString();
//Debug.Log(“SPAWNING”);
}
}
}

I can add also WaveSpawner script if it’s necessary. 

  • Liked by
Reply
3 on March 5, 2018

Yes if you could add the WaveSpawner, that would help.

I think the 1st problem, the count starting at 4, is because if you start the count at 5, and round the float down to an int, its gonna give you 4.

The 2nd one, the count restarting, I think is from your WaveSpawner state ENUM. Without seeing it I can’t be sure, but If your ENUM is getting reset to it’s counting state, that would just start it all over again.

on March 6, 2018

Thank you for the reply! Here is WaveSpawner CS

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WaveSpawner : MonoBehaviour {

public enum SpawnState { SPAWNING, WAITING, COUNTING};

[System.Serializable] // changeable in Unity
public class Wave
{
public string name;
public Transform enemy;
public int count;
public float rate;
}

public Wave[] waves;
private int nextWave = 0;
public int NextWave
{
get { return nextWave +1; } // +1?
}

public Transform[] SpawnPoints;

private float timeBetweenWaves = 5f;
private float waveCountDown;
public float WaveCountdown
{
get { return waveCountDown; }
}

private float searchCountDown = 1f;

private SpawnState state = SpawnState.COUNTING; // hidden in inspector
public SpawnState State
{
get { return state; }
}
void Start () {

if (SpawnPoints.Length == 0)
{
Debug.Log(“no spawnpoints refference”);
}

waveCountDown = timeBetweenWaves;

}

void Update () {
if (state == SpawnState.WAITING)
{
if (!EnemyIsAlive())
{
WaveCompleted();
}
else
{
return;
}
}

if (waveCountDown <= 0)
{
if (state != SpawnState.SPAWNING)
{
StartCoroutine(SpawnWave(waves[nextWave])); // starting spawning
}

}
else
{
waveCountDown -= Time.deltaTime;
}
}

void WaveCompleted()
{
state = SpawnState.COUNTING;
waveCountDown = timeBetweenWaves;

if (nextWave + 1 > waves.Length – 1)
{
nextWave = 0;
Debug.Log(“All waves complete”);
}
else
{
nextWave++;
}

}

bool EnemyIsAlive()
{
searchCountDown -= Time.deltaTime;
if (searchCountDown <= 0f)
{
searchCountDown = 1f;
if (GameObject.FindGameObjectWithTag(“Enemy”) == null)
return false;
}
return false; //true?
}

IEnumerator SpawnWave(Wave _wave)
{
Debug.Log(“SpawnWave” + _wave.name);
state = SpawnState.SPAWNING;

for (int i=0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1f / _wave.rate);
}

state = SpawnState.WAITING;

yield break;
}

void SpawnEnemy (Transform _enemy)
{
Debug.Log(“SpawningENemy” + _enemy.name);

if (SpawnPoints.Length == 0)
{
Debug.Log(“no spawnpoints refference”);
}

Transform _sp = SpawnPoints[Random.Range(0, SpawnPoints.Length)];
Instantiate(_enemy, _sp.position, _sp.rotation);
}
}

on March 6, 2018

Thank you.

 

So one problem, that it looks like you already know about, is your EnemyIsAlive check will always return false. You need to change it to what you have in the comments and have it return true if the timer isn’t up yet, or if we still find an enemy in the scene.

 

Other than that it looks like your states aren’t getting reset anywhere. You timer starting at 5 is whats causing it to start at 4 in the UI for the reason I mentioned earlier, just have it start at 6 for your desired effect.

 

 

I would fix the EnemyIsAlive issue, and then double check any values you set in the inspector.

on March 7, 2018

Thank you very much! it helped me!)))

 

Show more replies
  • Liked by
Reply
Cancel