Skip to content

Instantly share code, notes, and snippets.

@will-hart
Last active September 7, 2016 11:32
Show Gist options
  • Save will-hart/96f9071fbde93fe57ad9a92f82be7a4c to your computer and use it in GitHub Desktop.
Save will-hart/96f9071fbde93fe57ad9a92f82be7a4c to your computer and use it in GitHub Desktop.
Event driven version
///
/// PlayerManager.cs
///
using System;
public class PlayerManager : MonoBehaviour {
public event EventHandler PlayerDied;
/// ...
public void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Killzone")
{
var playerDiedSubscribers = PlayerDied;
if (playerDiedSubscribers != null)
{
playerDiedSubscribers(this, EventArgs.Empty);
}
}
}
}
///
/// GameManager.cs
///
using System;
public class GameManager : MonoBehaviour
{
/// ...
public void RespawnPlayer(object sender, EventArgs)
{
sender.PlayerDied -= RespawnPlayer;
var go = Instantiate(/*...*/);
go.GetComponent<PlayerManager>().PlayerDied += RespawnPlayer;
}
/// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment