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 /
This question was closed Mar 14, 2016 at 09:23 AM by hexagonius for the following reason:

Turned into bug report

avatar image
0
Question by hexagonius · Mar 08, 2016 at 03:20 PM · nullreferenceexceptioneventparameters

Unity accepts default parameter in event subscription but fails

I encountered a really strange problem with events. I seems that Unity's compiler has a problem when it comes to default parameters.

I have an Event:

 public static event System.Action<int> OnVisualLevelUp;

and I have a Function:

 public static void LevelAchievement(int playerLevel, System.Action potentialAbordAction = null, bool autoLogin = false)

Then I do this:

 LevelBar.OnVisualLevelUp += WK_GameCenter.LevelAchievement;

MonoDevelop complaints about that, but Unity does not, so I left it as is.

When the event is called, LevelAchievement does this and throws a NullReferenceException on the last line:

    if (potentialAbordAction == null){
       Debug.Log("potentialAbordAction is null");
       return;
    }
    potentialAbordAction ();


It actually reaches the function call, not returning on the check. I fixed it by adding the missing parameters to the event, but how is this even possible?

I would be very satisfied if somebody could explain that to me :)

Comment
Add comment · Show 3
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 Owen-Reynolds · Mar 08, 2016 at 05:29 PM 1
Share

I think this might be correct -- just regular C# rules. System.Action<int> is the delegate, which specifies 1 parameter. Your levelAchievement only matches it by slicing off the Default parms. StackOverflow has some Qs on this.

Is your fixed version still using System.Action-int, or did you rewrite it to have all three parms?

avatar image hexagonius Owen-Reynolds · Mar 09, 2016 at 09:50 PM 0
Share

I used all three. Thanks for the reply. I also thought that this is probably just a no go. Funny though, that Unity accepts it and compiles. but I created a bug report on this and it's uber investigation

avatar image noynac01 · Mar 08, 2016 at 09:41 PM 0
Share

Please include more information.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

37 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

Related Questions

Initialize UnityEvent via script 1 Answer

Reloading Scene causes "null" object references. 3 Answers

Animations couldn't be found but still plays 0 Answers

Unsubscribe events in OnDisable causes NullReferenceException 2 Answers

Animation Crossfading and Events (Legacy) not triggering at times 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