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. Dismiss Notice

Question Why can't I call a function with an array from a button?

Discussion in 'Scripting' started by coden2023, Jul 23, 2023.

  1. coden2023

    coden2023

    Joined:
    Jan 21, 2023
    Posts:
    2
    Hello, I am a bit new to unity, and I have noticed that unity does not allow you to "call" a function using the button. I am using a Text Mesh Pro button, and I am running version 2021.3.14f1 Personal. I might just be dumb, but I am new to this and I want to know if this is just me or if there is a reason behind this. upload_2023-7-23_13-45-8.png
    upload_2023-7-23_13-45-41.png
    If I remove the [], I can call the function from the button again.
     

    Attached Files:

  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Unity's Event System doesn't have a Property Drawer for that type of data, you can however, assign the Button's method through code and pass it your method no problem. The Unity Documentation has an example of how to do this, which I also posted below.

    Code (CSharp):
    1. // To use this example, attach this script to an empty GameObject.
    2. // Create three buttons (Create>UI>Button). Next, select your
    3. // empty GameObject in the Hierarchy and click and drag each of your
    4. // Buttons from the Hierarchy to the Your First Button, Your Second Button
    5. // and Your Third Button fields in the Inspector.
    6. // Click each Button in Play Mode to output their message to the console.
    7. // Note that click means press down and then release.
    8.  
    9. using UnityEngine;
    10. using UnityEngine.UI;
    11.  
    12. public class Example : MonoBehaviour
    13. {
    14.     //Make sure to attach these Buttons in the Inspector
    15.     public Button m_YourFirstButton, m_YourSecondButton, m_YourThirdButton;
    16.  
    17.     void Start()
    18.     {
    19.         //Calls the TaskOnClick/TaskWithParameters/ButtonClicked method when you click the Button
    20.         m_YourFirstButton.onClick.AddListener(TaskOnClick);
    21.         m_YourSecondButton.onClick.AddListener(delegate {TaskWithParameters("Hello"); });
    22.         m_YourThirdButton.onClick.AddListener(() => ButtonClicked(42));
    23.         m_YourThirdButton.onClick.AddListener(TaskOnClick);
    24.     }
    25.  
    26.     void TaskOnClick()
    27.     {
    28.         //Output this to console when Button1 or Button3 is clicked
    29.         Debug.Log("You have clicked the button!");
    30.     }
    31.  
    32.     void TaskWithParameters(string message)
    33.     {
    34.         //Output this to console when the Button2 is clicked
    35.         Debug.Log(message);
    36.     }
    37.  
    38.     void ButtonClicked(int buttonNo)
    39.     {
    40.         //Output this to console when the Button3 is clicked
    41.         Debug.Log("Button clicked = " + buttonNo);
    42.     }
    43. }
     
    coden2023, Bunny83 and spiney199 like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Note that when you try to subscribe methods with static arguments in the inspector, Unity only supports a very limited set of argument types. Also only a single argument is supported when static / serialized arguments are used. A UnityEvent uses this class internally to store that one single argument in the UnityEvent itself. As you can see it only supports a single bool, int, float, string or UnityEngine.Object reference as argument. Over here I explained the difference between dynamic and static arguments. Since the onClick event of a Button is a UnityEvent without any arguments, you can only link dynamic method which take no arguments at all, or you can subscribe any method that takes a single argument of the above mentioned supported types (bool, int, float, string or UnityEngine.Object).

    So your method can not be hooked up in the inspector as your arguments can not be serialized. Though as @RadRedPanda explained, you can of course subscribe a parameterless closure through code to that event. Any arguments you may want to pass to your actual method need to be specified there in code.
     
    RadRedPanda and coden2023 like this.