Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

UI Doesn't Accept Functions with more than two Arguments or Arrays?

Discussion in 'Unity UI & TextMesh Pro' started by kenaochreous, Mar 4, 2015.

  1. kenaochreous

    kenaochreous

    Joined:
    Sep 7, 2012
    Posts:
    395
    I'm confused, I was able to get my button to work with a function with one argument. But when I add a second argument or an Array I could no longer find the function. What gives?

    Code (CSharp):
    1. Public Void ExampleA(int someIntC){
    2. //Able to find ExampleA
    3. }
    4. Public Void ExampleB(int someIntC,int someIntD){
    5. // Not Able to find ExampleB
    6. }
    7. Public Void ExampleC(int[] someInts){
    8. // Not Able to find ExampleC
    9. }
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    504
    The inspector only lets you assign callback functions that take zero or one arguments, and there are only a few allowed types for the argument (bool, int, float, and GameObject, I think).

    But you can always write a wrapper function that calls your main function with whatever arguments you like.

    If you're feeling overly constrained by Unity's event system, you can also write your own C# events with any method signature you like--but the inspector doesn't support them, so you'll have to assign the listeners in code.
     
    rakkarage likes this.
  3. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,607
    The property drawer only supports a single parameter unfortunately.

    If you bind it through code you should be able to use multiple parameters in the signature.

    The event is based off UnityEvents and UnityActions, so up to 4 parameters are supported but only using c#base types (doesn't include arrays) If you want to make your own custom event type, then I would implement using these to keep conformity with the UI system.
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    504
    Are there advantages to using Unity's event system other than access to the property drawer?
     
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,607
    Only in so much as you can craft your events how you like or prefer them but the properties are still limited to Base C# data types and Unity objects.

    The new event system does have one big advantage, that is uses a clever weak referencing system under the hood which is more efficient and safe than fixed references and the ghastly SendMessage
     
  6. Dameon_

    Dameon_

    Joined:
    Apr 11, 2014
    Posts:
    504
    Isn't System.object one of the arguments you can pass? If so, then you just pass a custom class with as many fields as you want to have arguments.
     
  7. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,607
    @Dameon_ You can try it but all my tests seems to be inline with what was said at Unite on the new UI.
     
  8. tapticc

    tapticc

    Joined:
    Jan 16, 2014
    Posts:
    352
    I had a more simplistic approach to this, no doubt to be frowned upon but it worked for me!

    I called 2 functions on the gameobject in the OnValueChanged section; the first call passes a string variable and sets this in the class instance, and the second call passes in a slider instance. I can then use the previously set string name and the slider value in the second call's function.
     
  9. ebubeud1996

    ebubeud1996

    Joined:
    Mar 31, 2018
    Posts:
    5
    If you want to pass multiple parameters in a function, you can pass them as a single string, separating the values with a comma ",".
    For example, if you want to pass in 2, 3,4 in a single function, do it this way:

    In the Unity Editor, Set the Params for the function:
    test("2,3,4")

    //in your c# script
    public void test(string params){
    //split the params
    string[] splittedParams = params.Split(',');

    //get the first param
    string FirstParam = splittedParams[0];

    //Convert it back to int
    int firstParam = int.Parse(FirstParam);

    Debug.Log(firstParam);
    //2
    }
     
  10. junedmmn

    junedmmn

    Joined:
    Dec 19, 2016
    Posts:
    3
    This isn't the answer, what if I want to pass GameObjects such as which Button Pressed etc?