• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed May 20, 2018 at 09:43 PM by lery22.
avatar image
0
Question by lery22 · May 19, 2018 at 11:38 PM · uigameobject.active

Unity UI Panels can't use gameobject.activeInHierarchy?

I'm trying to deactive and re-activate certain panels in my UI. I've been trying to figure out what I've been doing wrong for some time but right now I'm not sure what to do anymore.

I'll start with my UI setup: alt text


With the following piece of code I'm trying to deactive and activate the highlighted object above:

 public Button playerMenuButton;
     // set with the editor by dragging and dropping my button onto the public field.
 public GameObject playerMenu;
     // set with the editor by dragging and dropping my InGameMenu onto the public field.

 void Start () 
 {
     playerMenuButton.onClick.AddListener(openPlayerMenu);
 }
 
 private void openPlayerMenu()
 {
     Debug.Log (playerMenu.activeInHierarchy);
     if (playerMenu.activeInHierarchy == false) 
         {
             playerMenu.SetActive(true);
         }
     else {
             playerMenu.SetActive(false);
             Debug.Log (playerMenu.activeInHierarchy);
     }
 }
         
 

However when I execute this code it deactives my object but doesn't let me activate it again. I've gone through multiple forums and websites trying to figure out what is wrong but maybe my knowledge of the unity editor or the panels is just lacking.


To show you what happens I added 2 debug.log lines which in turn made this an even bigger mystery to me about what is happening: alt text The first time I click my UI button it does what I want it to do. It deactives the panel and the Debug.log shows that the panel is first active (true) and after passing through my if statement it turns to false. However, If I click my button a second time the if statement deals the same way with the UI panel. Apparently it's still active and therefore it turns it to false once more. Why does this happen?


Not sure how or why I added a 3d gameobject in the public field in the editor. Surprisingly the code now executes exactly how I want it to be. It turns my object off and it turns back on after clicking my button a second time. So 3d objects do work but UI elements do not?


The problem seems to be in the gameObject.activeInHierarchy statement. Since it is able to read the active state of 3d objects but not the active state of UI objects.


Am I doing something wrong? Is it because the UI panel has a parent/children? Can someone help or teach me about this problem?


PS: adding a bool variable in script that hardcodes the state of the UI panel makes it able to active the UI object after deactivating it. The problem is the way it's trying to read the active state of the UI Object with gameObject.activeInHierarchy


Is this a bug? Or is my knowledge simply lacking in terms of the unity UI? Thank you for your help in advance! :)

untitled-2.jpg (28.7 kB)
untitled-3.jpg (48.5 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lery22 · May 20, 2018 at 09:47 PM 0
Share

Could moderators please delete this question.

1 Reply

  • Sort: 
avatar image
0

Answer by winterfluxstudio · May 20, 2018 at 12:08 AM

  playerMenuButton.onClick.AddListener(openPlayerMenu);


You need a reference to the button component in order to add a listener

Button PlayerMenuButton = playerMenuButton.GetComponent();

  public Button playerMenuButton;
  public GameObject playerMenu;
 
  void Start () 
  {
      Button PlayerMenuButton = playerMenuButton.GetComponent<Button>();     
      PlayerMenuButton.onClick.AddListener(openPlayerMenu);
  }
  
  private void openPlayerMenu()
  {
      Debug.Log (playerMenu.activeInHierarchy);
      if (playerMenu.activeInHierarchy == false) 
          {
              playerMenu.SetActive(true);
          }
      else {
              playerMenu.SetActive(false);
              Debug.Log (playerMenu.activeInHierarchy);
      }
  }
      




surely you just want to do this when setting the panel as active? not sure what else you're trying to do when the button is clicked.

      public Button playerMenuButton;
      public GameObject playerMenu;
     
      void Start () 
      {
          Button PlayerMenuButton = playerMenuButton.GetComponent<Button>();     
          PlayerMenuButton.onClick.AddListener(openPlayerMenu);
      }
      
      private void openPlayerMenu()
      {
          playerMenu.SetActive(true);
       }
 

Try this

 public GameObject Screen;
 
 public Button ExampleButton;
 
 // this button goes on the screen
 public Button CloseScreen;
 
 void Start()
 {
     Button exampleButton = ExampleButton.GetComponent<Button>():
     exampleButton.onClick.AddListener(OpenScreen);
 
     Button closeScreen = CloseScreen.GetComponent<Button>():
     closeScreen.onClick.AddListener(CloseScreen);
 }
 
 
 public void OpenScreen()
 {
     Screen.SetActive(true);
 }
 
 public void CloseScreen()
 {
     Screen.SetActive(false);
 }


This is a copy/paste of the documentation from Unity. Yet, you refuse to accept that you have to reference the button component, not just public Button MyButton

There is literally nothing I can do to help if you refuse to look at raw evidence. https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html

 //To use this example, attach this script to an empty GameObject.
 //Create two buttons (Create>UI>Button). Next, click your empty GameObject in the Hierarchy and click and drag each of your Buttons from the Hierarchy to the Your Button and "Your Second Button" fields in the Inspector.
 //Click the Button in Play Mode to output the message to the console.
 
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Example : MonoBehaviour
 {
     //Make sure to attach these Buttons in the Inspector
     public Button m_YourButton, m_YourSecondButton;
 
     void Start()
     {
         Button btn = m_YourButton.GetComponent<Button>();
         Button btn2 = m_YourSecondButton.GetComponent<Button>();
         //Calls the TaskOnClick method when you click the Button
         btn.onClick.AddListener(TaskOnClick);
 
         m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); });
     }
 
     void TaskOnClick()
     {
         //Output this to console when the Button is clicked
         Debug.Log("You have clicked the button!");
     }
 
     void TaskWithParameters(string message)
     {
         //Output this to console when the Button is clicked
         Debug.Log(message);
     }
 }
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lery22 · May 20, 2018 at 12:16 AM 0
Share

Check the button variable type. It is already a button so why would I need it's component? (I tried your solution nonetheless but this doesn't solve anything unfortunately). What I want is a simple switch. Pressing the button once opens the player menu, pressing it a second time closes it again :)

Thank you for your help :)

avatar image winterfluxstudio lery22 · May 20, 2018 at 12:21 AM 0
Share

because public Button player$$anonymous$$enuButton is a reference to the UI button in the hierarchy, not it's button component. You need to get the buttons component in order to deter$$anonymous$$e if it was clicked.

avatar image lery22 winterfluxstudio · May 20, 2018 at 12:28 AM 0
Share

I think you're referring to gameobject.GetComponent(). I debugged it to be sure but in both cases (set in the variable or through your method) I end up with the same type. It was a button before I used getcomponent and it's a button after using getcomponent. Also, after using getcomponent it changes nothing about the behaviour of the script that's not able to re-activate my UI object after deactivating it. The button doesn't seem to be the problem.

Show more comments
avatar image winterfluxstudio lery22 · May 20, 2018 at 12:25 AM 0
Share

updated answer with an example

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scrolling one UI image causes every other UI image to start scrolling 1 Answer

Healthbar does not follow camera/appears behind gameObjects 0 Answers

UI Elements in Screen Space Overlay obey Transform.Translate, but Elements in Screen Space Camera do not. 0 Answers

UI Text to Display Loading Progress of Songs in Music Folder 1 Answer

Scaling RectTransform to fit screen size in code 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges