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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Enable & Disable Canvas using script?

Discussion in 'UGUI & TextMesh Pro' started by circleline, May 16, 2015.

  1. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    Hey I have a script that goes like this,

    if (balldeath.death == true) {


    GameObject.Find ("Canvas").SetActive(true);


    }

    but unfortunately it doesn't work, if i set it to false it does work.

    P.S. i'm using UnityEngine.UI

    thanks
     
  2. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    GameObject.Find() will not find objects that are not active. That is, it will ONLY find objects that are active and in the scene.
     
  3. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    Any alternatives?
     
  4. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Depending on the structure of your scene you could keep a reference to the Canvas you're wanting to enable as a member of a game object (perhaps on the object you're calling Find from now).

    To do this create a public member like:

    public Canvas canvasReference;

    And then in the editor drag the canvas object onto that field in the inspector.

    Then rather than use Find, just do:

    canvasReference.SetActive(true);

    Another option would be to have the object active in the scene and then do a Find in Start() to get the reference and set it inactive but save the reference in a private class member for when you want to make it active.

    There are lots of options but all essentially revolve around getting a reference and storing it somewhere.
     
  5. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    I don't know why but i did what you said but i still can't get it to work..
     
  6. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Sorry but I can't help with "can't get it to work". You'll need to show your code (in code blocks please) and explain what you have done and what "didn't work" (ie what errors did you get, what did happen, etc).
     
  7. circleline

    circleline

    Joined:
    Nov 29, 2014
    Posts:
    42
    Oh, now i got it! Thanks! your method works!
     
  8. larku

    larku

    Joined:
    Mar 14, 2013
    Posts:
    1,422
    Excellent!