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
1
Question by Macro · Aug 07, 2012 at 05:15 PM · c#gameobjectevents

Do C# events work in unity?

I have got one gameobject with a script attached which exposes 2 events, LevelLoaded and LevelUnloaded, the ValueEventHandler is just an extended EventHandler which accepts a type for the event args to carry over.

 public class CurrentLevelBehaviour : MonoBehaviour
 {
     public event ValueEventHandler<ILevel> LevelLoaded;
     public event ValueEventHandler<ILevel> LevelUnloaded;
     // Methods which raise events
 }

Then I have another gameobject with a behaviour which is a HUD of sorts, so when a level is loaded it needs to know to update and display the information, however for some reason the callback is never raised.

 public class LevelHud : MonoBehaviour
 {
     private ILevel _currentLevel;
 
  // Use this for initialization
  void Start ()
  {
      var currentLevelBehaviour = GameObject.Find("CurrentLevel").GetComponent<CurrentLevelBehaviour>();
         currentLevelBehaviour.LevelLoaded += (sender, eventArgs) => { _currentLevel = eventArgs.Value; };
         currentLevelBehaviour.LevelUnloaded += (sender, eventArgs) => { _currentLevel = null; };
  }
  
  // Other methods which use _currentLevel
 }

So is this a unity problem? as there are no errors from the compiler or from unity when running things, I can just see that the level is not being set in the HUD area.

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 hideouswill · Aug 07, 2012 at 07:31 PM

The simple answer is yes, .NET events work in Unity. I use them widely in my game.

What might be at issue is when your events are getting raised. Empirically, I've observed that OnLevelWasLoaded() executes before either Start() or Awake(). If CurrentLevelBehavior raises the events from OnLevelWasLoaded(), your handlers have not yet been added to the events. I've found judicious use of Debug.Log() to be a good way to diagnose these ordering issues.

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 hideouswill · Aug 27, 2012 at 06:07 PM 0
Share

Odd--I got an email swearing that one $$anonymous$$rDude posted a comment on this answer asking for some clarification, but it does not seem to appear here. Let me quote the email here.

The question: How do you manage to declare your events?

I am doing this: public class wussEvent : EventArgs {

and am rewarded with:

error CS0246: The type or namespace name `EventArgs' could not be found. Are you missing a using directive or an assembly reference?

According to the docs, EventArgs is supported in all build platforms, it complains that EventArgs.EventArgs does not exist.

So it clearly does see it because it complains about the nested value, so I decided to test this theory and said using System.EventArgs and then it complains that

error CS0138: `System.EventArgs' is a type not a namespace. A using namespace directive can only be applied to namespaces

A type? So why then does public class myclass : EventArgs reply with "not found" ????

And the Oracle replies:

EventArgs is declared in the System namespace. I suspect you just need to add "using System;" to the top of your file, and you'll be good to go.

avatar image Bunny83 · Aug 27, 2012 at 06:50 PM 0
Share

@hideouswill: I can't reproduce this error. The only way for me to get this error is to not use the System namespace. However you should avoid importing the system namespace, since there are some intersections of UnityEngine and System (specifically Random and Object). It's better to just use the full class name:

 public class myclass : System.EventArgs
 {
     //...

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

10 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

Related Questions

Using script's method from all of the gameobjects that has that script 2 Answers

C# Check If Scripted Gameobject goes Past Variable Gameobject 0 Answers

why not run animation when doublication gameObject ???? 0 Answers

insert script question 1 Answer

Destroy Gameobject once 0 health 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