Hey all…
I’m having several issues with this lecture and I’ve gone through the video 4 or 5 times and still cannot figure out what I’m doing wrong.
I have a prefab created for my ‘Level Win Canvas’ and it’s turned on initially. When I play the level, it doesn’t turn off until I click play again to stop the level. At that point, the Level Win Canvas toggles itself off. When I play the next time, it toggles itself back on after I click play and then stop.
To help try to diagnose the problem, I put in some logging right before SetActive turns on and off and I’m seeing that in the console at the right time. It just won’t turn on and off the canvas for some reason.
I’ve also checked the Level Controller. That has my Scene Loader and my Level Controller scripts/components attached. My Level Win Canvas is in the Level Controller script/component as well.
I’m just not sure what I’m doing wrong here.
Attaching a picture of Unity that has my Level Controller and my logs. Also attaching some code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LevelController : MonoBehaviour
{
int numberOfAttackers = 0;
bool levelTimerFinished = false;
[SerializeField] GameObject winCanvas;
// [SerializeField] AudioClip levelComplete;
// [SerializeField] int levelCompleteVolume = 1;
[SerializeField] float waitToLoad = 3f;
private void Start()
{
// levelTimerFinished = false;
winCanvas.SetActive(false);
Debug.Log("winCanvas set as off");
Debug.Log("Level Started! Number of attackers: " + numberOfAttackers);
}
public void AttackerSpawned()
{
numberOfAttackers++;
Debug.Log("Attacker Spawned! Number of attackers: " + numberOfAttackers);
}
public void AttackerKilled()
{
numberOfAttackers--;
Debug.Log("Attacker Killed! Number of attackers: " + numberOfAttackers);
if (numberOfAttackers <= 0 && levelTimerFinished == true)
{
StartCoroutine(HandleWinCondition());
}
}
public void LevelTimerFinished()
{
levelTimerFinished = true;
StopSpawners();
}
private void StopSpawners()
{
AttackerSpawner[] spawnerArray = FindObjectsOfType<AttackerSpawner>();
foreach (AttackerSpawner spawner in spawnerArray)
{
spawner.StopSpawning();
}
}
IEnumerator HandleWinCondition()
{
Debug.Log("winCanvas set as on");
winCanvas.SetActive(true);
GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(waitToLoad);
// FindObjectOfType<SceneLoader>().LoadNextScene();
Debug.Log("Level Complete");
}
}