Created
July 17, 2017 16:20
-
-
Save JonathanYin/db3d2748d6bc9f36f81385f458d7ca44 to your computer and use it in GitHub Desktop.
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
using UnityEngine; | |
using System.Collections; | |
using UnityEngine.SceneManagement; | |
public class Destroyer : MonoBehaviour | |
{ | |
public GameObject player; | |
public GameObject player2; | |
void OnTriggerEnter2D(Collider2D other) | |
{ | |
//if the object that triggered the event is tagged player | |
if (other.tag == "Player1" && player2 == null) | |
{ | |
SceneManager.LoadScene(2); | |
} | |
else if (other.tag == "Player2" && player == null) | |
{ | |
SceneManager.LoadScene(2); | |
} | |
if (other.gameObject.transform.parent) | |
{ | |
Destroy(other.gameObject.transform.parent.gameObject); | |
} | |
else | |
{ | |
Destroy(other.gameObject); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since my game is an infinite-runner, and there is a camera moving along at a set pace, I had to make two rectangular colliders which used this script to destroy any objects they ran into. This would destroy game objects if they left the screen area, so that there wouldn't be too many objects cluttering the game memory, and making so that the players couldn't fall behind and expect to live. In addition, if a player fell onto a destroyer, and the other player was also dead, the game would go to the "GameOver" screen.