So I making a game with a timer but I don’t want to end the game when the timer runs out I want the user to lose a life so is there a way to do this? I have lives in the Player.cs but I can’t seem to implement it into the GameManager.cs code.
This is my GameManager.cs:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour
{
public int timeForLevel = 20;
private CountdownTimer myTimer;
public Player player;
private void Start()
{
myTimer = GetComponent<CountdownTimer>();
myTimer.ResetTimer(timeForLevel);
// print ("Timer started");
}
private void Update()
{
CheckGameOver();
}
// GAME OVER if seconds < 0 !!!!!
private void CheckGameOver()
{
int secondsLeft = myTimer.GetSecondsRemaining();
if(secondsLeft == 0)
{
player.lives--;
}
}
}
And this is my CountdownTimer.cs:
using UnityEngine;
using System.Collections;
public class CountdownTimer : MonoBehaviour
{
private float countdownTimerStartTime;
private int countdownTimerDuration;
public int GetTotalSeconds()
{
return countdownTimerDuration;
}
public void ResetTimer(int seconds)
{
countdownTimerStartTime = Time.time;
countdownTimerDuration = seconds;
}
public int GetSecondsRemaining()
{
int elapsedSeconds = (int)(Time.time - countdownTimerStartTime);
int secondsLeft = (countdownTimerDuration - elapsedSeconds);
return secondsLeft;
}
public float GetProportionTimeRemaining()
{
float proportionLeft = (float)GetSecondsRemaining() / (float)GetTotalSeconds();
return proportionLeft;
}
}
Am I supposed to put it in the GameManager.cs code?
And this is my Player.cs code just in case you need to see it:
using UnityEngine;
using System.Collections;
/**
\for the player character
\author Michelle Ruth
\date 2014
\warning not finished, health needs to be added and time
*/
public class Player : MonoBehaviour {
private CountdownTimer myTimer;
private int score = 0;
private int lives = 3;
private int DEATH_Y = -10;
public Texture2D LivesLeft1;
public Texture2D LivesLeft2;
public Texture2D LivesLeft3;
public int GetScore(){
return score;
}
public int GetLives()
{
return lives;
}
private void Awake()
{
myTimer = GetComponent<CountdownTimer>();
}
private void Update()
{
float y = transform.position.y;
if (y < DEATH_Y) {
MoveToStartPosition();
lives--;
}
if (score == 10)
{
Application.LoadLevel("Level2");
}
if (lives <= 0)
{
Application.LoadLevel("GameOver");
}
}
private void OnGUI()
{
GUILayout.BeginHorizontal ();
DisplayLives();
int secondsLeft = myTimer.GetSecondsRemaining();
string timeMessage = "Seconds left = " + secondsLeft;
GUILayout.Label(timeMessage);
string scoreMessage = "Score = " + score;
GUILayout.Label (scoreMessage);
}
private void DisplayLives()
{
int playerLives = GetLives();
if (1 == playerLives) {
GUILayout.Label(LivesLeft1);
}
if (2 == playerLives)
{
GUILayout.Label(LivesLeft2);
}
if(3 == playerLives){
GUILayout.Label(LivesLeft3);
}
}
private void MoveToStartPosition()
{
Vector3 startPosition = new Vector3(0,5,0);
transform.position = startPosition;
}
/**
* what increases the score
* anything with the tag Hidden
*/
private void OnTriggerEnter(Collider c)
{
string tag = c.tag;
if("Hidden" == tag)
{
score++;
}
if("Incorrect" == tag)
{
audio.Play ();
}
}
}
In the GameManager.cs where it says player.lives–; is the main part where I want to know if I can do this or if there is a particular way in doing it