Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
10
Question by Prosto_Lyubo · Mar 07, 2015 at 12:02 AM · uiinspectorevent

How to fire UnityEvent with parameters passed programmatically

Hi! I've created my own selectable with an UnityEvent onValueChange. I programmatically invoke it with parameter but it seems that listeners receive parameter set in inspector rather than the one sent in Invoke call. Is there a way to set which argument should be passed? Or may I somehow get into that value from inspector to change it on the fly?

This is part of my setup:

alt text

Functions SetRightLaneCount(int count) are fired with count set to 0 or whatever is set in that field in inspector.

But I fire the event manually:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System;
 using UnityEngine.Events;
 
 public class IncrementalInput : Selectable
 {
     [SerializeField]
     private Text text;
     [SerializeField]
     private Button PlusButton;
     [SerializeField]
     private Button MinusButton;
     public int MinValue;
     public int MaxValue;
 
     private int currentValue = 1;
     public int CurrentValue
     {
         get { return currentValue; }
         set { currentValue = value;
             if( CurrentValue == MaxValue )
                 PlusButton.interactable = false;
             else if( CurrentValue == MinValue )
                 MinusButton.interactable = false;
             else {
                 MinusButton.interactable = true;
                 PlusButton.interactable = true;
             }
             onValueChanged.Invoke(currentValue); //Event is fired
             text.text = value.ToString();
         }
     }
 
     [System.Serializable]
     public class OnValueChanged : UnityEvent<int> { };
     public OnValueChanged onValueChanged;
 
 
     public void Add()
     {
         if( CurrentValue < MaxValue )
             CurrentValue++;
     }
 
     public void Substract()
     {
         if( CurrentValue > MinValue )
             CurrentValue--;
     }
 
 
 }


So everything works fine: currentValue changes, events are fired, but don't use correct variable as an argument.

unityeventoverride.png (9.7 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by Prosto_Lyubo · Mar 23, 2015 at 11:14 PM

Well, it was after all pretty simple. Functions cannot be static. If they have a form described in the Documentation there will be two functions on the list with the same name! One is in section with dynamic parameters functions and the other in section of static parameters functions. If You want to fire UnityEvent like the one with my code all You have to do is to choose the one from dynamic parameters section and everything will work perfectly. Event will be fired with parameters passed in the Invoke method.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SirBernhart · May 27 at 01:39 PM 0
Share

Sorry to necro, but just to make it clearer what the difference is, here's an image: On the left is the "Dynamic" parameter and on the right is the "Static" parameter option!

alt text

explicacao-parametros-unityevent.png (36.8 kB)
avatar image
6

Answer by Brijs · Jun 09, 2016 at 12:04 PM

Suppose you declared unityevent as follows

 [System.Serializable]
     public class ToggleButtonClickCallBack : UnityEvent<bool>{
     }
     public ToggleButtonClickCallBack OnToggle = new ButtonClickCallBack ();
 

Now OnToggle Unity Event will be shown in inspector with a bool parameter if you select static parameter method.

If you invoke OnToggle event with dynamic parameter it will only use parameter set in inspector and discard dynamic parameter that we passed at invocation time.

For that I have got a solution…..

When you want to call OnToggle event with dynamic parameter use Unityevent class methods as follows to call all the registered methods with dynamic parameter value as you want.

 for(int i = 0 ; i < OnToggle.GetPersistentEventCount();i++ ){    
             ((MonoBehaviour)OnToggle.GetPersistentTarget(i)).SendMessage(OnToggle.GetPersistentMethodName(i),dynamicValue);
 }

 

Advantage of this is you can set a default value in inspector and call simply invoke to use default value and when you want dynamic parameters you can call as described above.

You can apply some filters also.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gamedev_fantasy · Apr 30, 2020 at 12:12 PM 0
Share

@Brijs do you know if this works nowadays? I'm doing something similar, but I get errors at runtime where Unity complains about the cast:

     void SetValue<T>(UnityEvent e, T newVal)
     {
         string methodName = e.GetPersistent$$anonymous$$ethodName(0);
         $$anonymous$$onoBehaviour mono = ($$anonymous$$onoBehaviour) e.GetPersistentTarget(0);
         mono.Send$$anonymous$$essage(methodName, newVal);
     }
avatar image WSWhitehouse gamedev_fantasy · Jan 26 at 12:36 PM 0
Share

@gamedev_fantasy yes, it still works - I've tested it in version 2021.2.5f1. I copied the original code snippet from the answer and had no issues. I realise this is an old thread, but wanted to answer your question just in case someone else wants an answer.

Thanks for the answer @Brijs! I can't believe this is what you have to do to invoke a unity event with a dynamic value!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get the default scene GUI for a CustomEditor for RectTransform? 1 Answer

How to access the OnClick event via Script 1 Answer

How to add OnClick listener in script to see it in inspector? 1 Answer

IndexOurOfRange: Array index out of range. 0 Answers

Can't drag slider onto inspector 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges