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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour {
public float startingTime;
private Text theText;
void Start () {
theText = GetComponent<Text>();
}
void Update (){
//response = 0;
float startTime = Time.time;
startingTime -= Time.deltaTime;
if (startingTime <= 0) {
Destroy (gameObject, 16);
Application.LoadLevel("Game_Over");
}
theText.text = "" + Mathf.Round (startingTime);
}
}
Score
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Score_System : MonoBehaviour {
public int pointsToAdd;
public void Start(){
}
// Use this for initialization
public void onClick () {
Score.AddPoints(pointsToAdd);
}
// Update is called once per frame
void Update () {
}
}
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?
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…
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
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.
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:
public class timer : MonoBehaviour
{
public float startTime;
[HideInInspector]
public float currentTime;
// Use this for initialization
void Start ()
{
currentTime = startTime;
StartCoroutine (CountDown ());
}
IEnumerator CountDown()
{
while (currentTime > 0f)
{
yield return new WaitForSeconds(1f);
currentTime -= 1f;
Debug.Log("Time: " + currentTime);
if(currentTime == 0)
{
//Start your GameOver Method
}
}
}
}
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).
public class score : MonoBehaviour
{
public int ScoreBonus;
public timer timerClass;
private bool answerCorrect;
// Use this for initialization
void Start ()
{
//If you forgot to add in Inspector the timer class
if (timerClass == null)
timerClass = (timer)FindObjectOfType (typeof(timer));
}
void onClick ()
{
//Check if the answer was wrong
CheckCorrect ();
if (answerCorrect == false) {
timerClass.currentTime -= 1f;
}
else
{
//Add here the Bonus Points
}
}
void CheckCorrect()
{
//Do here your stuff here to check if answer is correct
//and set either the answerCorrect to true or false
}
}
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.
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.
@garrido86 Thanks buddy. Your solution worked. Had To spiff it up a little but its working. These are my codes now.
CountDownTimer
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class CountDownTimer : MonoBehaviour {
public float startingTime;
private Text theText;
void Start () {
theText = GetComponent<Text>();
}
void Update(){
startingTime -= Time.deltaTime;
if (startingTime <= 0) {
Destroy (gameObject, 16);
Application.LoadLevel("Game_Over");
}
theText.text = "" + Mathf.Round (startingTime);
}
}
LoseTime Code
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Lose_TIme : MonoBehaviour {
public CountDownTimer timerClass;
private bool answerCorrect;
// Use this for initialization
void Start ()
{
//If you forgot to add in Inspector the timer class
if (timerClass == null)
timerClass = (CountDownTimer)FindObjectOfType (typeof(CountDownTimer));
}
public void onClick ()
{
//Check if the answer was wrong
timerClass.startingTime -= 1f;
//Add here the Bonus Points
}
}
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.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Score_Wrong : MonoBehaviour
{
public static int score;
Text text;
void Start()
{
text = GetComponent<Text> ();
score = 0;
score = PlayerPrefs.GetInt ("CurrenttScore");
}
void Update()
{
if (score < 0)
score = 0;
text.text = "" + score;
}
public void onClick(){
if (score == 4)
Application.LoadLevel ("Game_Over");
text.text = "" + score;
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
PlayerPrefs.SetInt ("CurrenttScore", score);
}
public static void Reset()
{
PlayerPrefs.SetInt ("CurrenttScore", score);
score = 0;
}
}
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.