0% found this document useful (0 votes)
72 views2 pages

Falling Apart - Destroybycontact Script

Uploaded by

api-478656110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views2 pages

Falling Apart - Destroybycontact Script

Uploaded by

api-478656110
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Falling Apart – DestroyByContact Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DestroyByContact : MonoBehaviour


{
private GameController gameController;
//Lives
private int lives = 3;
public GameObject star1, star2, star3, camera1;
private Animator animator;
public AudioSource audioSource;
public AudioSource audioSource1;

void Start()
{
GameObject gameControllerObject = GameObject.FindWithTag("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent<GameController>();
}
if (gameController == null)
{
Debug.Log("Cannot find 'GameController' script");
}

animator = GetComponent<Animator>();
}

void Update()
{
//Establishes the lives system
switch (lives)
{
//Displays 3 Lives
case 3:
star1.gameObject.SetActive(true);
star2.gameObject.SetActive(true);
star3.gameObject.SetActive(true);
break;
//Displays 2 Lives
case 2:
star1.gameObject.SetActive(true);
star2.gameObject.SetActive(true);
star3.gameObject.SetActive(false);
break;

//Display 1 Life
case 1:
star1.gameObject.SetActive(true);
star2.gameObject.SetActive(false);
star3.gameObject.SetActive(false);
break;

//Display 0 Lives & End Game


case 0:
star1.gameObject.SetActive(false);
star2.gameObject.SetActive(false);
star3.gameObject.SetActive(false);
Destroy(gameObject);
gameController.GameOver();
audioSource1.Play();
break;
}
}
private void OnTriggerEnter2D(Collider2D other)
{
//Player loses life and Impact Sound plays
if (other.tag == "Hazard")
{
lives = lives - 1;
audioSource.Play();
animator.SetTrigger("HitFlash");
}
}

You might also like