Canvas still visible after it was disabled by script at the start of a scene

Hello,

I am working on a AR-scene where I have several canvas that are supposed to be disabled after initializing the scene. When a user clicks on a cube within that scene, the canvas ,that also includes a UI sprite, shall become visible again and provide the user with some information.

This is the code I used:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ToggleMenu : MonoBehaviour
{

    // Invisible and clickable cube that contains the 3D-Model of the fountain
    public GameObject fountaincube = null;

    // Canvas that appears when the user clicks on the cube
    public Canvas fountaininfo;

    // Switch for visible / not visible setting
    public bool fountainswitch;

    // Button, that closes the canvas / makes it invisible again
    public Button fountainclose;


    // #####################
    // ## Settings at the start ##
    // #####################

    void Start()
    {
        // Assinging the Canvas to this variable
        fountaininfo = fountaininfo.GetComponent<Canvas> ();

        // visibility setting at the start
        fountaininfo.enabled = false;

        // setting of the switch
        fountainswitch = false;
    }


    // ######################################################
    // ## Function, that makes the button visible on click ##
    // ######################################################

    private void OnMouseDown()
    {
        if (fountainswitch == false)                      
            fountaininfo.enabled = true;
            fountainswitch = true;
    }


    // ############################################
    // ## Function, that closes the canvas again ##
    // ############################################

    public void fountainclose_Press()
    {
        fountaininfo.enabled = false;
        fountainswitch = false;
    }

}

However 2 out of 4 canvas are still visible after the scene initialized and have to be closed manually :frowning:
What am I missing here? The 4 canvas are exatly the same in the hierachy. I have also provided a screenshot from the scene and the editor (Editor_Screenshot_1)

Anyone can help? If you need further information, feel free to ask.

You misspelled your bool declaration

Sorry, that was an typo from the translation - the actual script is written in German and has no typos.

But still, that woulndt explain why only 2 out of 4 canvas are invisible although they are completly the same.

Any other ideas?

It’s difficult for me to tell what’s going on in the photo. Are you generating multiple instances of the object through code?
If not, you have set up the inspector to receive an input canvas. you can use the assigned canvas through its variable without having to attach it through GetComponent. This is the confusing part:

 // Canvas that appears when the user clicks on the cube
    public Canvas fountaininfo;
   ....
   fountaininfo= fountaininfo.GetComponent<Canvas>();

You are declaring a Canvas in the inspector, and then assigning the canvas from that canvas itself. Your Canvas exist on the Cube Object, and therefor could be grabbed through code by accessing it through your cube object. I’ve also had success with the SetActive(bool) method for Canvases. I edited your code, but haven’t had time to test it. Let me know how it goes.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ToggleMenu : MonoBehaviour
{
    // Invisible and clickable cube that contains the 3D-Model of the fountain
    public GameObject fountaincube = null;
    // Canvas that appears when the user clicks on the cube
    public Canvas fountaininfo;
    // Switch for visible / not visible setting
    public bool fountainswitch;
    // Button, that closes the canvas / makes it invisible again
    public Button fountainclose;
    // #####################
    // ## Settings at the start ##
    // #####################
    void Start()
    {
   //Removed this due based upon inspector cacheing
        // Assinging the Canvas to this variable
       // fountaininfo = fountaininfo.GetComponent<Canvas> ();
          // setting of the switch
        fountainswitch = false;
        // visibility setting at the start
        fountaininfo.SetActive(fountainswitch);
    }
    // ######################################################
    // ## Function, that makes the button visible on click ##
    // ######################################################
    private void OnMouseDown()
    {
             fountainswitch = !fountainswitch     
            fountaininfo.SetActive(fountainswitch);
   
    }
}

This should disable the canvas as long as you attach it in the inspector. Once again, if you are generating these GOs with a piece of code, you will have to assign the canvas to the cube through code with a GetComponent call. If you are planning on generating through code, I would suggest using a prefab with the canvas being a child of the specific cube that it is calling, as to simplify things a little bit.

In the above code, the OnMouseDown works to both activate and deactivate the canvas, if you want to split up the functionality, you can use an if statement to make the function only work upon a certain condition, and write an identical second function (like the one you had) to close the canvases.

Good luck!

1 Like

Many many thanks for looking into this mate - I´ll give it a go later on and let you know how it turned out! :slight_smile:

Okay, with your questions in mind, i´ll try to create a clearer picture here - sorry for that, this is only my 2nd project in Unity3D and there´s still plenty to learn about this tool (and C#).

“Are you generating multiple instances of the object through code?”

  • No, there is nothing, that is generated by code. Instead, I manually assign my canvas in the inspector. I thought the canvas has to be declared first before I start working with it. I´ll try working without the delaration and see how it turns out.

This is my situation in the hiearachy:

I have a AR-scene with a AR-scene-root and several canvas (No.Can) with each containing a sprite and a button, that closes / disables the canvas.

The hierachy has to be this way - if the canvas were part of the “scene root”-tree, my whole UI gets messed up (weird coordinates, shifted images and more). As you can see, the invisible cube carries the script.

This image shows the 4 AR-scenes in the scene view. Each scene contains a 3D-Model of a fountain and a invisible cube that is clicable.

The game view on the right shows one of the canvas that turns visible, once the invisible cube is clicked on. The X on the bottom is the button, that closes / disables the canvas.

Unfortunately, Unity3D does not seem to know how to handle the setActive method - I keep getting this message in the console:

“…Type UnityEngine.Canvas' does not contain a definition for SetActive’ and no extension method `SetActive’ of type…”

So I looked into the scripting reference (chapter Canvas) …

… but couldnt find an alternative to the “.enabled” method, which I used in the first place. :frowning:

Could we be dealing with a bug here? - Or am I just missing something?
I cant explain why only 2 out of 4 canvas are invisible after the scene was loaded

Hey I looked into it a little bit, and it turn out that you can’t call setactive on a Canvas, only a GameObject. I’ll rewrite it when i get a second to type some code and think it through

Issue resolved - All it needed, was to put the code from the start function, to the awake-function