- Home /
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.
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.
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.
@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
![](https://koobas.hobune.stream/wayback/20220613075323im_/https://answers.unity.com/themes/thub/images/avi.jpg)