Search Unity

Question Wrong canvas is enabled?

Discussion in 'Scripting' started by CodeMonkey757, Feb 10, 2024.

  1. CodeMonkey757

    CodeMonkey757

    Joined:
    Oct 27, 2023
    Posts:
    3
    I have a scene with 2 objects that enable different canvases. If I push Object A, Canvas A is supposed to be active. If I push Object B, Canvas B is supposed to be active. However, Object A enables Canvas B, and Object B enables Canvas B. And they do enable the correct canvas in the coding and in the inspector. What's going on? Why is this happening?

    I have the canvases set as GameObjects because when I try to set them as Canvases and use .enabled, they don't work. They only work as GameObjects with SetActive. And typing as in:

    public GameObject ExitCanvas;


    as opposed to

    public Canvas ExitCanvas;
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,841
    Do you just have the wrong object referenced?

    Right because a Canvas is not a GameObject, though like every component, it has a
    .gameObject
    property you can use to access the game object said component is attached to. So you'd do
    ExitCanvas.gameObject.SetActive()
    instead.
     
  3. CodeMonkey757

    CodeMonkey757

    Joined:
    Oct 27, 2023
    Posts:
    3
    Nope, it's the correct objects both times.

    Thanks, I'll try that instead.