I watched the Unity Space Shooter video tutorials and then I have added a second player. I can not seem to get the 'Game Over' when they both die. It will happen when one player dies but not both. Below is my code.

using System.Collections;

public class DestroyByContact : MonoBehaviour 
  public GameObject explosion;
  public GameObject playerExplosion;
  public GameObject player2Explosion;
  public int scoreValue;

  private bool P1Dead;
  private bool P2Dead;
  private GameController gameController;

  void Start ()
    P1Dead = false;
    P2Dead = false;
    GameObject gameControllerObject = GameObject.FindWithTag ("GameController");

    if (gameControllerObject != null)
      gameController = gameControllerObject.GetComponent < GameController> ();

    if (gameController == null)
      Debug.Log ("Cannot find 'GameController' script");

  void OnTriggerEnter(Collider other)
    if (other.CompareTag ("Boundary") || other.CompareTag ("Enemy"))

    if (explosion != null)
      Instantiate (explosion, transform.position, transform.rotation);

    if (other.CompareTag ("Player"))
      Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
      P1Dead = true;

    if (other.CompareTag ("Player2")) 
      P2Dead = true;
      Instantiate (player2Explosion, other.transform.position, other.transform.rotation);

    if (P1Dead == true & P2Dead == true)
      gameController.GameOver ();

    gameController.AddScore (scoreValue);
  • \$\begingroup\$ Style point: generally it's considered poor style to compare Booleans to a constant (if(P1Dead == true)), in preference to using the value directly (if(P1Dead)). \$\endgroup\$ – Clockwork-Muse Oct 31 '15 at 11:14

if (P1Dead == true & P2Dead == true)

That looks like a typo. Performing bitwise "and" instead of logic &.

Try this:

if (P1Dead == true && P2Dead == true)

  • 2
    \$\begingroup\$ Lynx eyes, that you have :P \$\endgroup\$ – Vaillancourt Oct 31 '15 at 3:40
  • \$\begingroup\$ It doesn't make any difference though, since bitwise and has the same effect as logical and for booleans. \$\endgroup\$ – FINDarkside Oct 31 '15 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.