• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
-1
Question by TheSparkM3llark · Apr 26, 2014 at 06:49 PM · gameobject

Lose a life when timer runs out

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheSparkM3llark · Apr 26, 2014 at 08:41 PM

I'm sorry to write on this again but is there anyone able to help I desperately need to know how to do this

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by mafio89 · Apr 26, 2014 at 09:21 PM

Hi,

Have you considered using StartCoroutine.

Example :

 private float expireTime = 5;
 private bool removeHp;
 
 void Start () {
         
         removeHp = false;
         StartCoroutine(StartRemoveHpAfterSomeTime());
     }
 
 IEnumerator StartRemoveHpAfterSomeTime() {
         //Wait for the timer to count up to the expireTime
         //and then start removing hp
         
         yield return new WaitForSeconds(expireTime);
         
         // You can then start another coroutine to remove the hp here
             // or work with the update function...

             removeHp = false;
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Attach rigidbody to ground below it 1 Answer

Assigning current color to a variable for fade out (C#) 0 Answers

Controlling gameObject inside another gameObject script 1 Answer

OnMouseEnter is Inconsistent 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges