Search Unity

Button with a variable

Discussion in 'Getting Started' started by emiter04boguslawski, Apr 6, 2021.

  1. emiter04boguslawski

    emiter04boguslawski

    Joined:
    Aug 12, 2020
    Posts:
    3
    Hi
    I've been programming in Unity recently so I'm a complete newbie.

    I would like to create a button that would be responsible for selecting the type of water.

    By clicking on the button, the next type would be selected and sent to the HexMapEditor as an int.

    How to do it?

    example code
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4.  
    5.  
    6. public class MltCapBtn : MonoBehaviour
    7. {
    8.     public int curVal = 0;
    9.     public Sprite[] sprites;
    10.     public GameObject Logo;
    11.     void Awake()
    12.     {
    13.         curVal = 0;
    14.  
    15.         // get second function and target object
    16.         Button btn = GetComponent<Button>();
    17.         var func = btn.onClick.GetFunction(1);
    18.         var target = btn.onClick.GetTargetObject(1);
    19.  
    20.         // set CurVal as parameter
    21.         btn.onClick.Function(1) = target.func(curVal);
    22.     }
    23.  
    24.     public void IfClick()
    25.     {
    26.         curVal++;
    27.         if (curVal >= sprites.Length)
    28.             curVal = 0;
    29.    
    30.         Logo.GetComponent<Image>().sprite = sprites[curVal];
    31.     }
    32. }
    33.  
     

    Attached Files:

    Last edited: Apr 6, 2021
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Wow, you are working way too hard to set up that button. All that code in your Awake method is not needed. Set up the button's onClick event in the inspector: just drag in the object that you want to handle it (probably whatever has this script on it, or maybe the HexMapEditor), and select the method that should handle the click. If that method takes a numeric parameter, you can type the value you want it to use right there in the inspector too.
     
    Peeter1978 and Vryken like this.
  3. emiter04boguslawski

    emiter04boguslawski

    Joined:
    Aug 12, 2020
    Posts:
    3
    I would like many time

    Func (range values)
    SetPlayer (-1 .. 7)
    SetWater (-1 .. 2)
    SetBrushSize (0 .. 4)
    SetElevationLevel (0 .. 5)
    SetHouse (0 .. 2)
    SetTower (0 .. 2)
    SetFarm (0 .. 2)

    Etc.. I think, universal method can be usefull, for next function in editor.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    We do click handler assignments at work, and it's fine to do it that way. But @JoeStrout was still correct in that you're making it way more complicated than it needs to be.

    Code (CSharp):
    1. // Assign references to these bad boys in the Inspector
    2. [SerializeField] private Button btnDoThatThang;
    3. [SerializeField] private Button btnDoAnotherThang;
    4.  
    5. void Awake() {
    6.     btnDoThatThang.onClick.AddListener(DoThang(1));
    7.     btnDoAnotherThang.onClick.AddListener(DoThang(2));
    8. }
    9.  
    10. private void DoThang(int thangToDo) {
    11.     // This is where you do your thang
    12. }
     
    Boodums, Peeter1978 and JoeStrout like this.
  5. emiter04boguslawski

    emiter04boguslawski

    Joined:
    Aug 12, 2020
    Posts:
    3
    Its not solve my problem.
     
    BigBoiAlex likes this.