Last active
September 7, 2016 11:32
-
-
Save will-hart/96f9071fbde93fe57ad9a92f82be7a4c to your computer and use it in GitHub Desktop.
Event driven version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// | |
/// 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