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

Invoking UnityEvent with argument

Discussion in 'UGUI & TextMesh Pro' started by Percy-Tse, Nov 28, 2014.

  1. Percy-Tse

    Percy-Tse

    Joined:
    Aug 22, 2014
    Posts:
    10
    Hi,

    I have declared a custom event by:
    Code (csharp):
    1.  
    2. // Class declaration
    3. [System.Serializable]
    4. public class MyEvent : UnityEvent<int> {}
    5.  
    6.  
    7. // Somewhere in a component
    8. // Declare
    9. public MyEvent PressEvent;
    10.  
    11. // Invoke
    12. int someValue = 123;
    13. PressEvent.Invoke(someValue);
    14.  
    When Invoke is called, the argument received by callback function is always equals to the value showing in inspector.
    Is there any way to invoke with dynamic arguments?
    Thanks.
     
    pako, hopetolive, arufolo and 3 others like this.
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    HI. In the inspector for the element you will notice there are two sections. Static and Dynamic. Dynamic calls use the value from the invoke, and static use the value you have manually set. You need to point to the dynamic call for this to work.
     
    Hozgen90, S4ngi, spakment and 28 others like this.
  3. Percy-Tse

    Percy-Tse

    Joined:
    Aug 22, 2014
    Posts:
    10
    Thanks Tim. I found it.

    For more information, the static/dynamic option appears when selecting function.
    callback.jpg
     
    Geony, Sheckdev, Radivarig and 21 others like this.
  4. Geony

    Geony

    Joined:
    Nov 28, 2020
    Posts:
    1
    thanks! i wondering about this problem in several days
     
  5. Hozgen90

    Hozgen90

    Joined:
    Jan 20, 2021
    Posts:
    16
    This worked, thanks a lot!