Search Unity

Reduce 1 Second OnClick From Timer?

Discussion in 'Scripting' started by shawnrevels, Jul 1, 2015.

  1. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    I have a timer set to a text. The timer works fine. I also have a score system that works fine. I want it to where the player clicks a wrong answer and it reduces the time by one second. Ive searched every where for a solution and ive spent hours writing my own code to try and work it out but im stuck. any help is appreciated.

    Timer

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6.  
    7.  
    8. public class CountDownTimer : MonoBehaviour {
    9.  
    10.  
    11.  
    12.     public float startingTime;
    13.  
    14.     private Text theText;
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.  
    22.     void Start () {
    23.  
    24.  
    25.         theText = GetComponent<Text>();
    26.     }
    27.  
    28.     void Update (){
    29.  
    30.         //response = 0;
    31.         float startTime = Time.time;
    32.  
    33.         startingTime -= Time.deltaTime;
    34.  
    35.         if (startingTime <= 0) {
    36.             Destroy (gameObject, 16);
    37.  
    38.  
    39.  
    40.                 Application.LoadLevel("Game_Over");
    41.                
    42.  
    43.         }
    44.  
    45.         theText.text = "" + Mathf.Round (startingTime);
    46.     }
    47.  
    48.  
    49. }
    Score

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Score_System : MonoBehaviour {
    6.  
    7.     public int pointsToAdd;
    8.  
    9.  
    10.     public void Start(){
    11.  
    12.     }
    13.                      
    14.  
    15.     // Use this for initialization
    16.  
    17.     public void onClick () {
    18.  
    19.  
    20.  
    21.  
    22.         Score.AddPoints(pointsToAdd);
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.    
    34.  
    35.     }
    36. }
    What I want is that when the player click the wrong button, which is set in the OnClick section of the Score script, they get a wrong point and the timer reduces by one. I tried Setting some code in the OnClick are of the score script to reduce the time but since the time is Static i dont think i can do this. Maybe i need a new timer script?
     
  2. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    The time isnt** static.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    your "Score_System" doesn't have any way to know what is a right or wrong answer... you'll need to add that. To update the timer you just need a reference to the CountDownTimer gameobject/script, Find and GetComponent will do that in script, or you could just link them in the inspector... hard to advise on the best method as you've not said how these buttons get created, or what type of game you're trying to make...

    http://docs.unity3d.com/410/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html


    you also have "startTime" and "startingTime" in the CountDownTimer. You are using startingTime in your ifs, but you never set it to anything so it'll be 0 from the beginning... I think you've got mixed up there and might need to have another look :)
     
  4. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Well the timer works fine. Starts at 15 seconds and counts down to 0. If zero is reached then the game over scene is called. the buttons are UI components. WIth an onClick function. Once clicked a point is either added to the right score or wrong score if theyve answered correct or wrong. One button in the script has a right answer and the others have a wrong answer. Hince why id like to subtract a second from the timer if the answer is wrong.
     
  5. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    If theyve answered correctly then a random scene is loaded.
     
  6. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    Well it seems like ive hit a dead end. Ill work on this later. Ive tried the getcompnent and didnt get anywhere lol. Thanks though @LeftyRighty
     
  7. garrido86

    garrido86

    Joined:
    Dec 17, 2013
    Posts:
    233
    Hmm.. its kinda hard to help because we don't get the whole picture but first off, don't use Update() for a simple timer, use a coroutine like this:

    Code (CSharp):
    1. public class timer : MonoBehaviour
    2. {
    3.     public float startTime;
    4.     [HideInInspector]
    5.     public float currentTime;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         currentTime = startTime;
    11.         StartCoroutine (CountDown ());
    12.     }
    13.    
    14.     IEnumerator CountDown()
    15.     {
    16.         while (currentTime > 0f)
    17.         {
    18.             yield return new WaitForSeconds(1f);
    19.             currentTime -= 1f;
    20.             Debug.Log("Time: " + currentTime);
    21.             if(currentTime == 0)
    22.             {
    23.                 //Start your GameOver Method
    24.             }
    25.         }
    26.     }
    27. }
    Also don't use static, if the field is public you can access it via GetComponent anyway and in Unity, static is something you rather like to use for helper classes (e.g. connecting player to a server).

    Code (CSharp):
    1. public class score : MonoBehaviour
    2. {
    3.     public int ScoreBonus;
    4.     public timer timerClass;
    5.     private bool answerCorrect;
    6.  
    7.     // Use this for initialization
    8.     void Start ()
    9.     {
    10.         //If you forgot to add in Inspector the timer class
    11.         if (timerClass == null)
    12.             timerClass = (timer)FindObjectOfType (typeof(timer));
    13.     }
    14.    
    15.  
    16.     void onClick ()
    17.     {
    18.         //Check if the answer was wrong
    19.         CheckCorrect ();
    20.         if (answerCorrect == false) {
    21.             timerClass.currentTime -= 1f;
    22.         }
    23.         else
    24.         {
    25.             //Add here the Bonus Points
    26.         }
    27.     }
    28.  
    29.     void CheckCorrect()
    30.     {
    31.         //Do here your stuff here to check if answer is correct
    32.         //and set either the answerCorrect to true or false
    33.     }
    34. }
    I hope this helps! Also one last hint: Never leave an Update method empty, if you don't need it. Reason is that an empty Update() will still be called by Unity and eat unnecessary cpu time.
     
    shawnrevels likes this.
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I can't see scripts you haven't mentioned, if you are setting startingTime from outside of the script you have posted above I can't see that.

    Again, no mention of how this assignment of right/wrong is being done is mentioned in the scripts you have posted or in the description you gave.


    We cannot see your project, we don't know what you have done beyond what you mention. If you want help with sorting out issues you need to give us all the information or you'll get answers which don't help because you have held back important details.
     
  9. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    @garrido86 Thanks buddy. Your solution worked. Had To spiff it up a little but its working. These are my codes now.

    CountDownTimer

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6.  
    7.  
    8.  
    9. public class CountDownTimer : MonoBehaviour {
    10.  
    11.  
    12.  
    13.     public float startingTime;
    14.  
    15.     private Text theText;
    16.  
    17.  
    18.     void Start () {
    19.  
    20.  
    21.         theText = GetComponent<Text>();
    22.     }
    23.  
    24.     void Update(){
    25.  
    26.  
    27.  
    28.  
    29.         startingTime -= Time.deltaTime;
    30.  
    31.         if (startingTime <= 0) {
    32.             Destroy (gameObject, 16);
    33.  
    34.  
    35.  
    36.                 Application.LoadLevel("Game_Over");
    37.                
    38.  
    39.         }
    40.  
    41.         theText.text = "" + Mathf.Round (startingTime);
    42.     }
    43.  
    44.  
    45.  
    46.  
    47.  
    48. }
    49.  
    LoseTime Code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Lose_TIme : MonoBehaviour {
    6.  
    7.     public CountDownTimer timerClass;
    8.  
    9.     private bool answerCorrect;
    10.    
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         //If you forgot to add in Inspector the timer class
    15.         if (timerClass == null)
    16.             timerClass = (CountDownTimer)FindObjectOfType (typeof(CountDownTimer));
    17.     }
    18.    
    19.    
    20.     public void onClick ()
    21.     {
    22.         //Check if the answer was wrong
    23.  
    24.             timerClass.startingTime -= 1f;
    25.  
    26.             //Add here the Bonus Points
    27.  
    28.     }
    29.    
    30.  
    31. }
     
  10. shawnrevels

    shawnrevels

    Joined:
    Aug 13, 2014
    Posts:
    86
    So now if they click the wrong button then they lose 1 second off the timer. Its a simple color button game @LeftyRighty . theres multiple buttons all with the same color but one. One button is slightly lighter in color and you have 15 seconds to choose the light colored button. The stages get harder and harder as you go. So i wanted to implement a solution so they cant just hit all the buttons and win all the time. So adding a system that takes away time was the best bet. I added code to where if they got so many answers wrong then the game ended, which was also a good route as well. this is the code for that incase anyone wanted it.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Score_Wrong : MonoBehaviour
    6. {
    7.     public static int score;
    8.    
    9.    
    10.     Text text;
    11.    
    12.    
    13.     void Start()
    14.     {
    15.         text = GetComponent<Text> ();
    16.        
    17.         score = 0;
    18.        
    19.         score = PlayerPrefs.GetInt ("CurrenttScore");
    20.     }
    21.    
    22.     void Update()
    23.     {
    24.         if (score < 0)
    25.             score = 0;
    26.    
    27.    
    28.  
    29.  
    30.  
    31.         text.text = "" + score;
    32.  
    33.  
    34.  
    35.    
    36.        
    37.     }
    38.  
    39.     public void onClick(){
    40.         if (score == 4)
    41.             Application.LoadLevel ("Game_Over");
    42.         text.text = "" + score;
    43.     }
    44.    
    45.     public static void AddPoints (int pointsToAdd)
    46.     {
    47.         score += pointsToAdd;
    48.         PlayerPrefs.SetInt ("CurrenttScore", score);
    49.     }
    50.    
    51.     public static void Reset()
    52.     {
    53.  
    54.         PlayerPrefs.SetInt ("CurrenttScore", score);
    55.         score = 0;
    56.     }
    57.    
    58.    
    59.    
    60.    
    61.    
    62.    
    63.    
    64.    
    65. }
    the onClick section is where i added the lose point system. This code is my wrong answer that applies to my incorrect score at the end of the game.