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 rself · Nov 03, 2015 at 11:08 AM · referenceeventspass

How to pass UnityEvent object as method Parameter

I'm building a tutorial system that has multiple steps. Each step has a certain event trigger, such as clicking on a button or completing the build of a structure. For simplicity sake, let's say I have the following code:

 public class GameController : MonoBehaviour {
 
     public UnityEvent gcEvent;
     public TutorialManager tm = new TutorialManager(); 
 
     void Start() {
         tm.SetupTutorial(gcEvent);
     }
 }
 
 public class TutorialManager {
 
     public UnityEvent tmEvent;
     
     public void SetupTutorial (UnityEvent passedEvent) {
         tmEvent = passedEvent;
         NextStep();
     }
     
     public void NextStep () {
         tmEvent.AddListener(NextStep);
     }
 }

In this case, passedEvent is always null instead of the UnityEvent object (because it's passing by value instead of reference?). Therefore, tmEvent is also null, and referring to tmEvent in NextStep() returns an error.

How can I pass UnityEvents between classes like this? This is driving me crazy....

Comment
Add comment · Show 6
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 meat5000 ♦ · Nov 03, 2015 at 11:07 AM 0
Share

Looks to me like a bunch of empty variables. Where are you expecting the data to come from?

The example in the docs shows you do not create a 'new' UnityEvent, you should Add the Listener in Start and most certainly not within the function it is linking.

Here's the page

http://docs.unity3d.com/ScriptReference/Events.UnityEvent.html

avatar image Baste · Nov 03, 2015 at 11:27 AM 0
Share

Your code sample does not throw any errors. This extension:

 public void SetupTutorial(UnityEvent passedEvent) {
     tmEvent = passedEvent;
     NextStep();
     Debug.Log("invoking");
     tmEvent.Invoke();
 }

 public void NextStep() {
     Debug.Log("calling next step");
     tmEvent.AddListener(NextStep);
 }

Prints: "calling next step" "invoking" "calling next step"

You can pass UnityEvents around as if they were any other object. Do you have a failing example?

@meat, I'm assu$$anonymous$$g the event is assigned in the editor.

avatar image ben-rasooli · Nov 03, 2015 at 12:52 PM 2
Share

Why your GameController class doesn't derive from $$anonymous$$onoBehaviour? Then how do you expect the Start() method in it to run?

avatar image meat5000 ♦ ben-rasooli · Nov 03, 2015 at 12:59 PM 0
Share

Good catch ;)

avatar image rself ben-rasooli · Nov 04, 2015 at 01:08 AM 0
Share

It does in my actual code, sorry I didn't put it in my simplified code above!

avatar image rself · Nov 04, 2015 at 01:07 AM 0
Share

So…it seems I have been duped by the $$anonymous$$onoDevelop debugger! One of the things I tried before posting this question was, as @meat5000 suggested, initialize gcEvent variable in the Start() method using “new UnityEvent();”. However, when I did this and stepped through the code, the debugger suggested in red “Object reference not set to an instance of an object” so I immediately reverted back to not initializing the variable (avoid red messages at all costs!).

That error message is somewhat incorrect/misleading apparently, as the event is actually working fine within my code now that I added back in the initialization statement. I guess they don’t consider a UnityEvent an object since it is a form of delegate?

Either way, thanks for the quick responses to the issue @meat5000 @Baste and @ben.rasooli !alt text

screen-shot-2015-11-03-at-74215-pm.png (6.3 kB)

0 Replies

· Add your reply
  • Sort: 

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

6 People are following this question.

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

Related Questions

Custom UnityEvent with ExposedReference object field 1 Answer

Where can I find complete list of events? 1 Answer

How to select an int/float variable from another script in the inspector? 2 Answers

Send javascript array to a ray hit object 1 Answer

How to seperate/decouple UI and player code 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