Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to enable a canvas from a script?

Discussion in 'Scripting' started by computerfreak, Jan 11, 2015.

  1. computerfreak

    computerfreak

    Joined:
    Dec 28, 2014
    Posts:
    15
    I try to make a game menue that only shows up when the player clicks on a game object. I have a main canvas which contains a child canvas. the child canvas contains my game menue. Now I would like to display this child canvas when the user clicks on a game object. The game object contains a script for the click. I tried following so far, but it doesn't work:
    Code (CSharp):
    1. GameObject CanvasPlayerGUI = GameObject.Find ("MyCanvas");
    2. CanvasPlayerGUI.SetActive(true);
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Does it work as you want it to when disabling 'MyCanvas' in the inspector?
    Is a NullReferenceException thrown? Or does it simply not react when clicking?

    You have to provide a bit more information.
     
  3. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
    Also why use find, and not just directly reference the canvas with a public game object field
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can't use Find to get inactive GameObjects. The simplest way to avoid this is to add a reference directly in the inspector.
     
  5. computerfreak

    computerfreak

    Joined:
    Dec 28, 2014
    Posts:
    15
    Thank you for the tips. I managed to make it work this way:
    Code (CSharp):
    1. Canvas[] CanvasPlayerGUIs = FindObjectsOfType<Canvas>();
    2.         foreach (Canvas CanvasPlayerGUI in CanvasPlayerGUIs)
    3.         {
    4.            
    5.             if(CanvasPlayerGUI.name == "MyCanvas")
    6.             {
    7.                 CanvasPlayerGUI.enabled = true;
    8.                 break;
    9.  
    10.             }  
    11.         }
     
  6. SevenHams

    SevenHams

    Joined:
    Dec 27, 2012
    Posts:
    67
    It's still far, far better to create a public Canvas and drag and drop the canvas into the script in the editor if you are creating it ahead of time. Things are different if you are creating them with a script but if you aren't the reference is the best option. Other than that yes, enabled = true is the way to turn it on.
     
    orchard800 and Kiwasi like this.
  7. computerfreak

    computerfreak

    Joined:
    Dec 28, 2014
    Posts:
    15
    And how can I do that?
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    In the Monobehavior class that is enabling and disabling...

    Code (csharp):
    1.  
    2. using UnityEngine.UI;
    3.  
    4. [SerializeField]
    5. private Canvas myCanvas; // or whatever you want to call it
    6.  
    7. //then in your functions you simply call
    8. myCanvas.enabled = true;

    Then in the inspector, on the object that has this code on it as a component, there should be a slot for MyCanvas. You can drag the Gameobject that has your Canvas on it into this slot, and you are all hooked up.
     
  9. grka

    grka

    Joined:
    Jan 14, 2015
    Posts:
    80
    How can I assign the Canvas reference to that variable when my gameobject is created dynamiclly through another script? When I use GameObject.Find("/Canvas/MyCanvas/") then I get a compiler error that it can't convert GameObject to UI
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Because you're probably trying to assign a gameObject to the variable 'myCanvas' in the sample code above, which is of type Canvas. You can try to use GetComponent<Canvas>() of the object that you found with the line you posted.