Compiler Warning CS0067: Event Never Used

I have an event that I am using, so I really don't understand what this warning really means. Can someone clarify?

public abstract class Actor<T> : Visual<T> where T : ActorDescription { #region Events /// <summary> /// Event occurs when the actor is dead /// </summary> public event Action Dead; #endregion /// <summary> /// Take damage if the actor hasn't taken damage within a time limit /// </summary> /// <param name="damage">amount of damage</param> public void TakeDamage(int damage) { if (damage > 0 && Time.time > m_LastHitTimer + m_DamageHitDelay) { m_CurrentHealth -= damage; if (m_CurrentHealth <= 0) { m_CurrentHealth = 0; if (Dead != null) Dead(); } else StartCoroutine(TakeDamageOnSprite()); m_LastHitTimer = Time.time; } } 

In my other class, I register and unregister for an event:

  if (m_Player != null) m_Player.Dead += OnPlayerDead; if (m_Player != null) m_Player.Dead -= OnPlayerDead; 
+5
source share
2 answers

Since the class Actor<T> is abstract, and the code inside the Actor<T> does not raise an event, you can make an abstract event:

 public abstract event Action Dead; 

Then, in the subclass (es) that inherits from Actor<T> , you override the event:

 public override event Action Dead; 

If the subclass does not actually raise an event, you can suppress the warning by giving the add and remove events empty (see this blog post ).

 public override event Action Dead { add { } remove { } } 
+9
source

The real problem is that I am using Unity and Mono 2.6, and this is still a bug. So I tried to use the sentence Blagboard said; It worked for Visual Studio, but not for the compiler.

So, there are two solutions that throw a #pragma warning about disconnecting around the event or pass a generic type.

So:

 public abstract class Actor<T> : Visual<T> where T : ActorDescription { #region Events /// <summary> /// Event occurs when the actor is dead /// </summary> public event Action<Actor<T>> Dead; #endregion ... private void SomeFunc() { if (Dead != null) Dead(); } } 
+1
source

All Articles