I am having trouble with getting a 2nd event I created to work. I just learned how to create my own events a few days ago, so maybe there is something I am not understanding about creating events? I thought that maybe I needed to create another delegate just for the onGameLevelIncrease, but that didn’t seem to do anything. Thanks in advance for any help!
I have two scripts
1: EventManager
2: HudScript
In the EventManager I created this event
1: onCoinCountIncrease
That works fine, no errors.
Then I added another event
2: onGameLevelIncrease
I then get this error:
Assets/Scripts/ObjectScripts/HudScript.cs(15,53): error CS0103: The name `onGameLevelIncrease’ does not exist in the current context
It complains about this line here in the HudScript
EventManager.onGameLevelIncrease += onGameLevelIncrease;
Code Below.
public class EventManager : MonoBehaviour
{
public delegate void PlayerStatsHandler(int count);
public static event PlayerStatsHandler onCoinCountIncrease;
public static event PlayerStatsHandler onGameLevelIncrease;
public GameManager gameManager;
public int newCoinCount;
public int currentCoinCount;
public int newGameLevel;
public int currentGameLevel;
void Start ()
{
gameManager = GameObject.Find("ScriptHolder").GetComponent<GameManager>();
currentCoinCount = gameManager.coinCountCollected;
currentGameLevel = gameManager.gameLevel;
}
void Update ()
{
newCoinCount = gameManager.coinCountCollected;
if (newCoinCount != currentCoinCount)
{
currentCoinCount = newCoinCount;
onCoinCountIncrease(currentCoinCount);
}
//code for onGameLevelIncrease event
newGameLevel = gameManager.gameLevel;
if (newGameLevel != currentGameLevel)
{
currentGameLevel = newGameLevel;
onGameLevelIncrease(currentGameLevel);
}
}
}
public class HudScript : MonoBehaviour
{
void OnEnable()
{
EventManager.onCoinCountIncrease += onCoinCountIncrease;
EventManager.onGameLevelIncrease += onGameLevelIncrease;
}
void OnDisable()
{
EventManager.onCoinCountIncrease -= onCoinCountIncrease;
EventManager.onGameLevelIncrease -= onGameLevelIncrease;
}
}