Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Setzer22 · Jan 14, 2013 at 02:07 PM · events

NullreferenceException with an Event

Hello everyone.

Something in my code has been bugging me for a couple of days. I can't seem to get it to work, I'll post some code and explain my problem:

 //C#
 public class Foo : MonoBehaviour {
 
     public delegate void EventHandler();
     public event EventHandler InitComplete;
 
     public void Init() {
 
        //Many Irrelevant stuff goes on here
 
        InitComplete();
     }
 }

So this is the code I've got. Whenever I play the game in the editor, I get a NullReferenceException at the line where I call to trigger the event InitComplete(). Telling me the usual message "Object reference not set to an instance of an object".

As I don't fully understand how events work. I can't seem to get what's going on here. If it isn't obvious with the code, I should say that I'm trying for the class to throw an event called InitComplete, at the end of the method Init().

What I find weird is that everything works fine except that event. No null references anywhere else. Actually, the class which is triggering the event has instances (and even if it didn't it shouldn't make a difference, simply the event wouldn't get triggered, wouldn't it?).

I'm kind of confused with all this. I'd appreciate some help.

Thank you very much.

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
16
Best Answer

Answer by CHPedersen · Jan 14, 2013 at 02:37 PM

This is because you have to check if the event is null before you trigger it:

 if(InitComplete != null)
     InitComplete();

The reason is that events are actually placeholders for a list of delegates to be called whenever the event is triggered. That's why you can use "+=" when you subscribe to an event - you're adding your handler to a list of methods that are going to be called. But if nobody has subscribed yet, (there is nowhere in your code where it says Foo.InitComplete += SomeHandler;) then the list of delegates to call will be empty, and trying to access it therefore causes a null reference exception.

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 Setzer22 · Jan 14, 2013 at 02:51 PM 0
Share

That explains it, I'm going to fix it and post with some feedback. Thank you!

EDIT: It worked, once again thank you. I'll mark this as answered

avatar image CHPedersen · Jan 14, 2013 at 02:55 PM 0
Share

No problem, glad I could help. :)

avatar image
0

Answer by pierrickian · Apr 18, 2014 at 12:45 PM

Hello, I encountered the same problem but the fact is I managed many other events that work well. But one of them is null (OnPlayersAroundTable) even if some gameObjects have subscribed to it. Could someone please help me ?

My code below:

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MyClass : MonoBehaviour
 {
     public static List<int> playersTable;
     public delegate void PlayersAroundTable (List<int> PlayersTable);
     public static event PlayersAroundTable OnPlayersAroundTable;
 
     public static void awake () {
         playersTable = new List<int> ();
         playersTable = GetPlayersTable ();    
         OnPlayersAroundTable (playersTable);
     }
 }
 
 public class MyOtherClass : MonoBehaviour
 {
 
     public List<int> playersTable;
 
     void Start () {    
         MyClass.OnPlayersAroundTable += InitPlayersAroundTable;
     }
 
     public void InitPlayersAroundTable (List<int> PlayersTable)
     {
         playersTable = PlayersTable;
     }
 }
 

If the method 'OnPlayersAroundTable (playersTable);' is in the 'void Start ()' of MyClass class, it works ! Someone can explain why ?

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 BlackPanda · Jan 11, 2016 at 09:12 AM 0
Share

Pretty sure help is not still needed, it might be because of the wrong case of awake() method. Use Awake() ins$$anonymous$$d. And please don't post any followed up question as an answer. Use comments like this for that.

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

12 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

Related Questions

Call parent guitexture mouse events 0 Answers

Detect when the Player starts 1 Answer

Can you get mecanim to not play events when animation isn't playing? 0 Answers

Unity hardware changed event 0 Answers

Custom Inspector: Unity Events 2 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