Canvas Not Displaying Even Though SetActive is Set to True

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");
    }


}

Hi Tim,

You mentioned a prefab. Prefabs are not in the scene, thus they are not visible in the game. Check your Win Canvas field in the Inspector. Does it reference a prefab or a game object in your Hierarchy?

Thanks Nina! That worked.

Can you explain a little more about why prefabbing those canvases won’t work? I would love to be able to prefab them so that I can pull them into future levels more easily. Maybe I missed something when we were talking about prefabs, but this had my brain going in circles yesterday.

Thanks again!

As aforementioned, prefabs don’t exist in the scene. If you want to see something in your scene/game, that something must be in the scene. Disabling/enabling a prefab doesn’t affect anything in your current scene.

You have got a canvas in your scene. You want to see that canvas, thus you must enable that specific canvas.

That’s the whole secret. :slight_smile:

1 Like

Thanks for the help on this one. Really appreciate the quick replies!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms