- Home /
When Score goes up by 25 change timer value
In my Game i want to make it for every 25 points the timer goes down by 0.25 seconds. How cn i do this with these 2 sets of codes?
using UnityEngine;
using UnityEngine.UI;
public class KeepScoreManager : MonoBehaviour
{
public Text ScoreText;
private int score;
public Text NewBest;
public void IncreaseScore(int increment)
{
NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
score += increment;
if (score > PlayerPrefs.GetInt("NewBest"))
{
PlayerPrefs.SetInt("NewBest", score);
NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
}
ScoreText.text = score.ToString();
}
}
and the timer;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountdownScript : MonoBehaviour
{
[SerializeField] private Text uiText;
[SerializeField] private float mainTimer;
[SerializeField] private GameObject panel;
void Start()
{
timer = mainTimer;
}
public float timer;
private bool canCount = true;
private bool doOnce = false;
void Update()
{
if (timer >= 0.0f && canCount)
{
timer -= Time.deltaTime;
uiText.text = timer.ToString("F");
}
else if (timer <= 0.0f && !doOnce)
{
canCount = false;
doOnce = true;
uiText.text = "0.00";
timer = 0.0f;
panel.SetActive(true);
}
}
public void ResetBtn()
{
timer = mainTimer;
canCount = true;
doOnce = false;
}
}
Comment
Answer by I_Am_Err00r · Jul 30, 2019 at 02:24 PM
using UnityEngine;
using UnityEngine.UI;
public class KeepScoreManager : MonoBehaviour
{
public Text ScoreText;
private int score;
CountdownScript countdown;
public Text NewBest;
private void Start()
{
countdown = GameObject.FindObjectOfType<CountdownScript>();
}
public void IncreaseScore(int increment)
{
NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
score += increment;
changeTime += increment;
if (score > PlayerPrefs.GetInt("NewBest"))
{
PlayerPrefs.SetInt("NewBest", score);
NewBest.text = PlayerPrefs.GetInt("NewBest").ToString();
}
ScoreText.text = score.ToString();
if(changeTime >= 25)
{
countdown.ChangeTime();
changeTime = 0;
}
}
}
And then somewhere in your CountdownScript, add this:
public void ChangeTime()
{
timer -= .25f //You could also assign a public variable in case you want this value different at times, but this should do what you are asking.
}
Sorry for late reply went on holiday however with the script it says the name ChangeTime does not exist in this context so will I need to do a gameobject.getcomponent or something to link that part of the keepscoremanager script
Your answer
Follow this Question
Related Questions
How to restart a level with countdown? 4 Answers
How to stop a countdown from counting down 1 Answer
countdown timer acivation 1 Answer
Lerpz Escapes question 1 Answer
countdown timer on for example a wall of a building in your game? 2 Answers