My UI Score Is Not Working?
Okay, when it comes to programming, I am a noob. I usually use Javascript even though I don't know jack squat about it, but I've sort of learnt how it works with the if statements and variables and all, but when I tried C#, which I was terrified of using, it went exactly how I thought it would.
So, I'm trying to get the UI working for my game, a cartoony-bloody border with score and high score text in the top left corner. I have a functioning GUI, but it looks ugly and I know I can do better. So I'm trying to set up the UI to display the score. The C# tutorial I watched had some other score system going on, there wall a ball hitting a rope and the score system was all in the one script, but I could make out some of the script and thought I could set the score to display a PlayerPref int whatever it's called.
But as you can see in the images below (the maximum is 2, so I put them all in 1) it didn't work. I've included the other scripts involving score.
So maybe someone could help me out? Also, the game involves driving a car around and running over pedestrians, just letting you know so you understand the gameplay I'm going for. And as an extra, could someone tell me how to get a countdown timer in the UI as well? For 60 seconds, BTW.
Thank you! :D
TLDR Version: Used C# for the first time, score UI doesn't work.
Could you post your code as code blocks in your question? It would be easier to help if we could copy-and-paste ins$$anonymous$$d of typing it all from scratch from the image.
Answer by KrabyGame · Nov 14, 2015 at 05:25 PM
First, assign a gameobject with a Text component on it to the scoreText variable (drag it from hierarchy to the inspector, in Unity), it should work
I think you should avoid using PlayerPrefs in Update / FixedUpdate/OnGUI or everything that is executed every frame
In scoreUI, you get the "yourScore" value from playerpref every Update (every frame) You should use the currentScore variable in scoreKeeper.js Same in OnGUI , replace the PlayerPrefs.GetInt(...) by highScore.
For the countdown you can add a float variable somewhere, let say scoreUI.cs (btw, why do you use both cs and js ?), set it to 60 in the Start function and in update, add
countDown -= Time.deltaTime;
deltaTime is the time in second between the last frame and the current one. Then make a timeText variable which will contains the text that display the time , and update it in Update It should looks like this
public class scoreUI : MonoBehaviour
{
public Text scoreText, timeText;
public float timeLeft;
void Start()
{
timeLeft = 0;
if (scoreText || timeText)
Debug.LogError("Assign Text gameobjects in the inspector");
}
void Update()
{
scoreText.text = "Score: " + scoreKeeper.currentScore;
timeText.text = "Time: " + timeLeft;
timeLeft -= Time.deltaTime;
}
}
Whoops! Heh, forgot something. I didn't set the score text thing in the component.
$$anonymous$$y game was actually playable, but the score still wasn't displaying. And I couldn't make out half of what you said, so I tried doing whatever I could read from that, but it didn't work. THE STRUGGLE! Ugh. I guess I should just put the GUI in the corner and just publish this frickin thing already.
N$$anonymous$$ I've got a functioning GUI now. I'll probably fix it later.
Your answer
Follow this Question
Related Questions
How add(create) a new gameobject on scene on mouse button click. 2 Answers
NullReferenceException: Object reference not set to an instance of an object ? 0 Answers
Need a Script for Changing Scenes after pressing E to open locked door once a key has been found. 0 Answers
UnityScript to C# Conversion 1 Answer