I’m finally getting around to using delegates and events in my code, after reading tons on the subject. But I can’t seem to accomplish what I’m going for. I have an “Entity” class which has this:
public class Entity : MonoBehaviour {
public delegate void EntityDelegateMessage();
}
One of its child classes has this:
public class AsteroidController : Entity
{
public static event EntityDelegateMessage EventAsteroidDestroyed;
}
I can call “EventAsteroidDestroyed” from within the AstetroidController class and my GameManager script subscribes to it like this:
AsteroidController.EventAsteroidDestroyed += AnotherOneBitesTheDust;
This fires the proper method in GameManager and all is well. Now, I would like to send another type of message from the AsteroidController class with an accompanying integer, who’s value depends on the instance of the class sending the message (not all asteroids are worth the same number of points). So I added this in Entity:
public delegate void EntityDelegateScore(int points);
public static event EntityDelegateScore EventScorePoints;
Now, in the AsteroidController, if I try to send the message using:
EventScorePoints(1);
Unity (and Visual Studio) complain with the following: “The event ‘Entity.EventScorePoints’ can only appear on the left hand side of += or -= when used outside of the type `Entity’”.
What am I doing wrong? I know that if I want the instance variable I shouldn’t use a static modifier but I don’t know how this should be done.
Thanks a lot.