Search Unity

Assigning an ID to Unity buttons

Discussion in 'Scripting' started by ericspataru, Feb 27, 2017.

  1. ericspataru

    ericspataru

    Joined:
    Jan 31, 2017
    Posts:
    54
    Question

    How to assign each one of my buttons an ID and how to access it?


    Other answers to the same question

    This question has been answered before, but only partially. I mean, one guy coded a method, but unfortunately it was written in java... Also, someone invented other method, but you couldn't access the ID via script, so it was pointless.


    What I've tried so far

    I created a script for each button called IDScript. In that script I wrote public static int ID=3; (or 1,2,4 whatever) and then when I want to access this ID from other script I don't exactly know what to write. It would be something like buttonWhichIHaveJustClickedOn.GetComponentInChildren<IDScript>() ???? or how should I write it in order to get the number?


    Thanks in advance for any help.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    What are you trying to do?

    If you really needed a custom button, you could subclass Unity's Button class and add a property for the data you want to set and query, then you wouldn't need an additional component.

    But I'd rather ask what prevents you from using the onClick event that a Unity Button provides? If you add listeners to a button, you usually know which button has been clicked, as you subscribe with specific handler for the event.

    Tell us what's the purpose for an additional ID and we'll see what we can do. :)
     
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    Having a static variable on your script means all your buttons share it. So each one that sets it will overWrite the others. Whoever sets it last, is what value it will have for ALL the buttons. You could try something like this:

    Code (CSharp):
    1. using System;using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonID : MonoBehaviour {
    6.  
    7.     static int id;
    8.     int myID;
    9.    
    10.     void Awake()
    11.     {
    12.         myID = id++;
    13.     }
    14. }
    But then you have no real way to tell which button got which ID. they will all have a unique one, but you don't really have a map to it.

    So you can add OnClick listeners in the inspector but its very limited. It can only handle 0 or 1 argument method. And if you want to pass an argument it has to be statically set in the inspector. Here is how I would setup your buttons:

    1. Create a ButtonEnum script, Just sits in your project do not attach to anything.
    Code (CSharp):
    1. using System;using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum ButtonEnum  {
    6.  
    7.     Play,
    8.     Shoot,
    9.     Start,
    10.     Stop
    11. }
    12.  
    2. Create an Empty GameObject call it ButtonHandler and attach this script to it:
    Code (CSharp):
    1. using System;using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonClickHandler : MonoBehaviour {
    6.  
    7.     public void ButtonClick(ButtonEnum buttonType)
    8.     {
    9.         switch (buttonType)
    10.         {
    11.             case ButtonEnum.Play:
    12.                 Debug.Log("You Pressed Play");
    13.                 break;
    14.             case ButtonEnum.Shoot:
    15.                 Debug.Log("You Pressed Play");
    16.                 break;
    17.             case ButtonEnum.Start:
    18.                 Debug.Log("You Pressed Start");
    19.                 break;
    20.             case ButtonEnum.Stop:
    21.                 Debug.Log("You Pressed Stop");
    22.                 break;
    23.         }
    24.  
    25.     }
    26. }
    27.  
    3. Create a Button and attach this script to it:
    Code (CSharp):
    1. using System;using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ButtonID : MonoBehaviour {
    7.  
    8.     public ButtonEnum myType;
    9.     public ButtonClickHandler handler;
    10.  
    11.     private void Start()
    12.     {
    13.         Button button = GetComponent<Button>();
    14.         button.onClick.AddListener(delegate { handler.ButtonClick(myType); });
    15.     }
    16. }
    17.  
    4. In the inspector drag ButtonHandler object onto the appropriate spot on the ButtonID script in the inspector.
    5. Choose from the DropDown what kind of button it is (This is its ID).

    Hit play and it will tell you what kind of button was pressed. This shows you how you can create a custom listener in the script. You can have your button clicks have as many arguments as you want using this method. If you don't want to use enums you can use integer IDs or strings or whatever you like.
     
  4. ericspataru

    ericspataru

    Joined:
    Jan 31, 2017
    Posts:
    54
    Thanks for the help guys! I fixed it using button tags. I think that I could also implement your solutions, but this came to my mind last night and I forgot to check the forums for answers. Thanks a lot for your effort and hopefully others will find your answers useful.