- Home /
How do you get a function to 'check' a previous function has occurred in order to trigger a second function?
Hello. I'm trying to figure out how to 'check' if a function has occurred so that it triggers the next function.
The basics are that my script either 'collects' (common box) when the player collides with the object, or 'blows up' if the player collides with a bomb. I've got two pieces of code, one which is placed on the player, and another on the camera.
What I was attempting to do with the second script is to check to see if the 'Destroy' function has occurred for the player, and if so, go through the revive code to check whether or not to respawn or Game Over.
This is my first Unity project and so far I've been going through tutorials online and the forums, but have drawn a blank on this particular issue.
Code on Player:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "CommonBox(Clone)")
{
Destroy(col.gameObject);
}
else if (col.gameObject.name == "bomb")
{
Destroy(gameObject);
}
Code on Camera:
private void revive ()
{
if (player == false)
{
if (currentHealth >= 1)
{
Instantiate(player, new Vector3(7, 2, -1), Quaternion.identity);
currentHealth = currentHealth - 1;
}
else if (currentHealth <= 0)
{
Instantiate(gameover, new Vector3(0, 0, -4), Quaternion.identity);
}
}
}
Hopefully this makes some sense, if I can provide any more information then please let me know.
Answer by RobAnthem · Feb 22, 2017 at 08:57 AM
Honestly, when it comes to things like this. There is always ways of doing it, I could tell you 3 different ways, but Unity already addressed the entire issue. The simplest answer is Events. Create an event, and subscribe to it.
Here is some Unity docs on the subject.