Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by eepatscott · May 05, 2020 at 10:34 PM · eventeventsactionevent-listenereventlistener

Can you use UnityEvents to invoke methods with parameters from other classes? how?,UnityEvents with arguments resulting in a lot of errors. UnityAction? how?

I'm a bit green when it comes to this... I've been trying to use UnityEvents to pass information between classes.

this page of the API leads me to believe that this is, in fact, possible: https://docs.unity3d.com/ScriptReference/Events.UnityEvent_1.html

when i tried doing this almost exactly, it didn't work, so i've been wrestling with it, but so far i've mostly been running into walls. here's code that follows that example almost exactly:

   public UnityEvent ChooseInteractionEvent;
 
     // Use this for initialization
     void Start()
         {
             
         if (ChooseInteractionEvent == null)
             ChooseInteractionEvent = new UnityEvent();
             //listen for interaction event
             ChooseInteractionEvent.AddListener(ActivateWheel);
         }
     public void ActivateWheel(InteractableObject target)
         {
             ActiveObject = target.gameObject;
        
        }  
 

similar to what is shown in the API, ActivateWheel is a method in this class which takes 1 argument, my goal is to activate it from another class.
The line where I add the listener is gave me the error "Argument 1: Cannot convert from 'method group' to UnityAction"
the research i did has been confusing as to what exactly the difference is and what the consequences of changing ActivateWheel to a unity action are.

the parameter, the InteractableObject "target" is the information i'm trying to pass between classes, so I can't get rid of it. I want to invoke the event from another class so I can pass that information to this class.

When the Event is invoked, the only argument i gave it is the target object, that line is also throwing an error.

 this.gameObject.GetComponent<localPlayer>().ChooseInteractionEvent.Invoke(hit.transform.gameObject.GetComponent<InteractableObject>());
 

that's the line where i'm invoking the event. but my attempt to invoke it throws the error that the "invoke" method can't take any arguments.

so, is it not possible to pass parameters through events? does it only work if it's within the same class? Do i need to use delegates to make events work between classes even with UnityEvents?

In case that method is moot, i tried about 3 other ways of doing it, so I may need to ask some followup questions about why those methods are also failing.

it's also possible that this is not how events with parameters work, since when i add to the UnityEvent, it seems to be okay, until i actually run the program, then it doesn't work :/

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

1 Reply

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

Answer by Hellium · May 05, 2020 at 11:07 PM

You are very close to the desired result. The last thing to do (change) is the type of the Unity event to actually have an event taking one parameter.

 [System.Serializable]
 public class InteractableObjectEvent : UnityEvent<InteractableObject> {}

Then replace public UnityEvent ChooseInteractionEvent; by public InteractableObjectEvent ChooseInteractionEvent; and you should be ready to go.

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 eepatscott · May 06, 2020 at 04:04 PM 0
Share

ahh, ok. thank you, that works perfectly! tysm for the help!

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

130 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make prefab that generated more than once listener in Unity Events 0 Answers

Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer

How to use UnityEvent with duplicate gameobjects? 1 Answer

Clear event System.Action list 1 Answer

EventTrigger / EventListener (what's the difference?) 0 Answers


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