Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by webicindia · Nov 25, 2016 at 06:36 AM · uibuttonsdisplay

show hide button in unity using c#

I have 15 buttons on my screen at onClick event am fetching the button and hiding that button using

EventSystem.current.currentSelectedGameObject.SetActive(false); now at the end i have to show buttons again so i am using

  for (int i = 0; i < 15; i++)
 {

     tag1 = "Button" + (i + 1);
     GameObject.FindGameObjectWithTag(tag1).SetActive(true);
     Debug.Log("done");

 }

Loop is giving error becuase it is not able to find object which has been hidden or setActive(false) from other references unity forum i see same solution by i don't know why it is not working in my case any help will be appreciated Thanks

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by dacarrera · Nov 25, 2016 at 09:50 AM

I think this could be simplified. In your hierarchy you can make an empty button manager and make all of your buttons children of it. Then just use GameObject.SetActive(true) to have them appear again. This would work well if you wanted to work with all the buttons at once.

However, if you're looking at making different buttons appear/disappear at different times, you should make an empty __Manager and set each button as a GameObject in a script to manage them.

 // Your buttons
 public GameObject button1;
 public GameObject button2;
 public GameObject button3;
 
 // function to enable 
 public void enableButton(onButton){
     onButton.SetActive(true);
 }
 
 // function to disable
 public void disableButton(offButton){
     offButton.SetAdtive(false);
 }

You can add the public functions to an on/off button.

Comment
Add comment · Show 1 · 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 webicindia · Nov 25, 2016 at 10:17 AM 1
Share

thanks @dacarrera your answer is right,just we have save these object references before hiding the object

avatar image
0

Answer by Dibbie · Nov 25, 2016 at 09:46 AM

What you could do, is put all your buttons under one gameObject you wont hide, just a simple transform, and child all your buttons to it - since its a UI, use a "Panel", and turn off the Image component of that panel so it acts like just a parent or "holder"... Then in your script, you can find that panel object instead, and loop through each of the children.

 for (int i = 0; i < GameObject.Find("PanelWithButtons").transform.childCount; i++){
 GameObject.Find("PanelWithButtons").transform.getChild(i).gameObject.SetActive(true);
 }

General idea, untested code.

Comment
Add comment · Show 2 · 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 webicindia · Nov 25, 2016 at 09:49 AM 1
Share

transform doesn't contain definition of getChild

avatar image Dibbie webicindia · Nov 25, 2016 at 09:52 AM 0
Share

Well the name "PanelWithButtons", is whatever name you gave the panel gameobject in the hierarchy, and it could also be GetChild, ins$$anonymous$$d of getChild as well.

avatar image
0

Answer by Denscrivent · Dec 01, 2016 at 08:21 AM

You can put the same tag names to the buttons and you just do this code GameObject[] buttons = GameObject.FindGameObjectsWithTag("Buttons");
foreach(GameObject button in buttons){
button.SetActive(False);
}

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Turn Another UI image on and off with a Single UI Button 1 Answer

Screen Space - Camera Bug (Buttons does not work) on Unity 2017 0 Answers

Changing the names of a list of Buttons all at once? 1 Answer

ui panel disappears after showing for few seconds ?(problem) 1 Answer

UI Relative to screen size 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