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

How Do I get A reference To A Button

Discussion in 'UGUI & TextMesh Pro' started by jroto23, Nov 28, 2015.

  1. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    In my canvas I have 4 buttons. When I click the first button I want to make 2 of the other buttons to become invisible. My script is on the canvas and all the onClick functions are in that script. So How can I get a reference to some specific buttons on that canvas so I can disable them? I am assuming I just set enable = false once I get the reference, but I don't know how to get the reference.

    EDIT:
    currently I am using FindGameObjectWithTag but then I have to create tags for every object. I can for see 100 tags. And I'm cool with that but I don't know if that is the best way, any help would be appreciated.
     
    Last edited: Nov 28, 2015
  2. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    Well, I feel stupid, I never realized there if just a Find() where I can use the name of the button. So I don't have to create a 100 tags
     
  3. Chichkan

    Chichkan

    Joined:
    Jun 15, 2015
    Posts:
    5
    Actually, there is a better way to get what you want. Just create in your script four public variables:

    public class ButtonsController : MonoBehavior {

    public Button Button1;
    public Button Button2;
    public Button Button3;
    public Button Button4;
    ................
    }

    After that go to Unity and select canvas with your script. You will see four new fields on it. Then just drag corresponding game objects (buttons) in this fields and you can use their in the script.
     
    cwilliams_emed likes this.
  4. jroto23

    jroto23

    Joined:
    Oct 19, 2015
    Posts:
    82
    Thanks, I am not used to this work flow and forgot about that.