- Home /
How can i link my code together so the score value affects the timer value
hello! In my game i want to link the timer and score scripts together so for every 25 points the timer value loses 0.25 seconds off of it. How can i link both these scripts together? ScoreText controls the text of Score and NewBest is simply for the high score
Here is "KeepScoreManager" (Score script)
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 here is the timer script, (a lot of it towards the bottom is for game over screens and other functions within the game)
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;
}
private 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);
}
}
Answer by GlassesGuy · Jul 28, 2019 at 12:05 AM
make a gameobject and call it whatever you want, then you can use GetComponent<>() to get access to your other script. Here's an example:
public class KeepScoreManager : MonoBehavior
{
public gameObject countDownGameobject;
public float threshhold = 25;
public float scoreDecrement = 0.25
private float relativeScore;
private int scoreDecrementCounter;
void Update()
{
relativeScore = score - threshold * scoreDecrementCounter;
if(threshhold >= score)
{
relativeScore = 0;
countDownGameobject.GetComponent<CountdownScript>().timer -= scoreDecrement;
scoreDecrementCounter ++;
}
}
}
Hope this helps!
@GlassesGuy So public game object countdown game object is the timer, and lets say the new component is called decrement I'd put in the script GetComponent() and then we'd be done?
In the code I have man game issues 1)the -.25 decrement says cant be converted into a float, use an F suffix 2) the name threshhold doesnt exist in this context 3) countdownscript.timer isnt accesible because of its protection level
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Decrement : $$anonymous$$onoBehaviour
{
public GameObject Score;
public GameObject Timer;
public float threshhold = 25;
public float scoreDecrement = 0.25;
private float relativeScore;
private int scoreDecrementCounter;
void Update()
{
relativeScore = Score - threshhold * scoreDecrementCounter;
if(threshhold >= Score)
{
relativeScore = 0;
Timer.GetComponent<CountdownScript>().Timer -= scoreDecrement;
scoreDecrementCounter ++;
}
}
}
Yes! Sorry I frequently make mistakes with code when I'm trying to go quick. Every time 0.25 is used it should be converted to 0.25f so that the game knows it's a float and not something else. On the "public float scoreDecrement = 0.25" part you need to add a semicolon on the end. On whatever gameobject you have $$anonymous$$eepScore$$anonymous$$anager script you need to set the gameobject in the editor, Just go to that gameobject and drag the gameobject with your Countdown script and drag it in. and you lastly need to change the "private float timer;" code in your Countdown script to "public float timer;" so that other scripts can access it. Sorry for the confusion, tbh i'm still learning C# and finding problems on these forums is a way for me to learn code better by solving problems I don't already know how to solve.
So how does it know what the score is because if we don't need it in the code then how does it know as one issue is I can't do the score