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

Buttons Issue

Discussion in 'Scripting' started by Onilut, Dec 23, 2015.

  1. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    I make a set of buttons on UI during runtime. When I first make them, they do an animation, where they bounce, and when they stop, I want these buttons to be clickable (each button have a script called SampleButton(), which has this bool clickable variable, when they finish the animation, clickable become true). I have a script that is called when I press a button. in this Script, i would like to check is clickable is already true on this button. How do I make a reference to the button clicked on this function.
     
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Give your SampleButton component from the clicked button as reference to the onClick function, so you can acces your boolean
     
  3. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Code (CSharp):
    1. void Start () {
    2.         PopulateList();
    3.     }
    4.  
    5.     void PopulateList()
    6.     {
    7.         float posX = -2.5f;
    8.         foreach (var obje in listCardsNeutrals)
    9.         {
    10.             GameObject newCard = Instantiate (sampleButton) as GameObject;
    11.             SampleButton cardButton = newCard.GetComponent<SampleButton>();
    12.             cardButton.nameLabel.text = obje.description;
    13.             cardButton.icon.sprite = obje.icon;
    14.             cardButton.button.onClick = obje.effect;
    15.             newCard.transform.SetParent (UiCanvas);
    16.             newCard.transform.position = new Vector3 ( posX, 15f, -10f);
    17.             newCard.transform.localScale = new Vector3 ( 1f, 1f, 1f);
    18.             posX += 2.5f;
    19.         }
    20.     }
    21.  
    22.     public void doEffect()
    23.     {
    24.             Debug.Log ("I want to poop right now");
    25.     }
    I made this. i got the obje.effect (the reference to the function onClick), and added to the instatieated button.OnClick. And then, when the button is pressed, it goes to doEffect() function. How do I do to check the boolean clickable in that function.
     
  4. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    You can do a cool c# trick:
    Code (CSharp):
    1. cardButton.onClick = () => { obje.effect(cardButton);};
    you just have to change the definition of effect function to suport Button input
     
    Polymorphik likes this.
  5. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Sorry but i'm a bit newbie at this. But what's the difference between "cardButton.button.onClick= obje.effect;" and the line you wrote (I didn't know how to write it correctly so Unity could recognize it, plus its the first time I see that line, so I don't understand what exactly does or how it helps me to look for clickable bool inside the SampleButton script in the button).
     
  6. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Well isn't effect a function?
    That code is something called Anonymus Function https://msdn.microsoft.com/en-us/library/bb882516.aspx
    Is like creating a function localy in which you have access to outside variables like cardButton.

    The thing is, you can't give a function with parameter to onClick, you have to give one which has no input. With this method you can give input parameter to your onClick function. So in your obje component you define a function, let's call it OnSampleButtonClick, which takes as input a button (public void OnSampleButtonClick(SampleButton butt)). Here you can do you click logic + you can check is butt is clickable. Now every time a button is click the { obje.effect(cardButton);} is called. Imagine it like a normal function, but because it was created this way, this function knows who obje and cardButton is.

    Is this clear enough?
     
  7. Onilut

    Onilut

    Joined:
    Aug 10, 2015
    Posts:
    36
    Code (CSharp):
    1. [System.Serializable]
    2. public class goodCards {
    3.     public string description;
    4.     public Sprite icon;
    5.     public Button.ButtonClickedEvent effect;
    6. }
    7. [System.Serializable]
    8. public class badCards {
    9.     public string description;
    10.     public Sprite icon;
    11. }
    12. [System.Serializable]
    13. public class neutralCards {
    14.     public string description;
    15.     public Sprite icon;
    16.     public Button.ButtonClickedEvent effect;
    17. }
    18.  
    19. public class CardManager : MonoBehaviour {
    20.  
    21.     public GameObject sampleButton;
    22.     public List<goodCards> listCardsGoods;
    23.     public List<badCards> listCardsBads;
    24.     public List<neutralCards> listCardsNeutrals;
    25.  
    26.     public Transform UiCanvas;
    27.  
    28.     // Use this for initialization
    29.     void Start () {
    30.         PopulateList();
    31.     }
    32.  
    33.     void PopulateList()
    34.     {
    35.         float posX = -2.5f;
    36.         foreach (var obje in listCardsNeutrals)
    37.         {
    38.             GameObject newCard = Instantiate (sampleButton) as GameObject;
    39.             SampleButton cardButton = newCard.GetComponent<SampleButton>();
    40.             cardButton.nameLabel.text = obje.description;
    41.             cardButton.icon.sprite = obje.icon;
    42.             cardButton.button.onClick = obje.effect;
    43.             newCard.transform.SetParent (UiCanvas);
    44.             newCard.transform.position = new Vector3 ( posX, 15f, -10f);
    45.             newCard.transform.localScale = new Vector3 ( 1f, 1f, 1f);
    46.             posX += 2.5f;
    47.         }
    48.     }
    49.  
    50.     public void doEffect()
    51.     {
    52.         //if (this.sampleButton.clickable)
    53.             Debug.Log ("I want to poop right now");
    54.     }
    55.  
    56.     // Update is called once per frame
    57.     void Update () {
    58.  
    59.     }
    60. }
    61.  
    Complete script.

    effect is not a function, or at least, not a function made by me. And effect have already a a designation to go to doEffect() function (and it does correctly). BTW I'm going to try what you wrote here, and i'll post again if i sucess, or if I did a mistake again.

    Edit: it does no work, basically because effect is not a function, its a button event.
     
    Last edited: Dec 23, 2015