- Home /
C#: Q about creating Score system using GUItext and Time
My game is a round maze where you need to roll the ball to the center. I need three score parameters:
How many times overall player succesfully rolled the ball to the center.
Fastest time
How many times player rolled the ball to the center,only with time less than 3 seconds, IN A ROW.
1) My code for first parameter
public class Reset2 : MonoBehaviour {
public GUIText Score; /i dragged specific GUIText object in/
private int Point;
void Start () {
/something/
}
void Update () {
if /something/
{
/reload level/
Point = 1;
Score.text="0" + Point;
}
}
}
First i need, is to show in my dragged specific GUIText object, how many times player rolled the ball to the center ( "if /something/" means that player sucessfully did it ). So GUIText, that shows it and start with "0", need to +1 every time "if /something/" triggers. But i am not sure what to change in my code, because it doesn't seem to work.
2) Fastest time. I've founded "Time.timeSinceLevelLoad" , but i am not sure how it works and should i use it or not? I need the time from the start of every session ( level ), to the end ( when the ball reachs the center, the "if/something/" part), and in case if the new time is better, it must replace GUIText for fastest time with a new record.
I guess for 2) i need to start timer counting in "Void Start" part, and in "Void Update" part i need to write something like "If new time < Record, /then/ GUIText.text = new time;"
3) Using "Time.timeSinceLevelLoad" for example, i want to record how many times IN A ROW, player succesfully rolled the ball to the center with time less than 3 seconds.
i guess i need to write in "Update part" something like
if player's time < 3 seconds then somehow set GUIText.text ="1"; if player's time is again < 3 seconds then set somehow GUIText.text="2"; if player's time is AGAIN < 3 seconds then set to 3 and etc. etc.
maybe even loop it somehow with increasing number
So, any idea how to make it right?
Answer by bubzy · Sep 03, 2014 at 08:30 AM
you need a firm idea of HOW exactly the player scores, are you using collisions or are you going to track the position? without this you are just going to be focussing on too many things at once.
Unity tracks time via Time.time
so you could
//in variable declaration
float playTime = 0f; //store start time
float attemptTime = 0f; //store total attempt time
int attempts = 0; // how many goes has the player had
//when player starts a new attempt
playTime = Time.time; // do not put this inside a loop or it will constantly update;
attempts ++;
//when player completes an attempt
attemptTime = Time.time - playTime; //this will give you time elapsed from the line "playTime = Time.time"
Score.text = "Attempts : "+ attempts + " Last Time : " + attemptTime"; //just a personal gripe about using capitalised letters for the first word of a variable, it can get confusing :)
//if you wanted to display the FASTEST time, you would need another variable to compare the time with and then store the lowest in a new float called 'fastestTime' or something like that.
all that being said, it really seems like you could do with brushing up a bit more on coding, might be worth trying to make a simple pong game or something first, as all the principles are there and you don't have to come up with any concepts, it would help you get a solid foundation in logic and the Unity engine, then you can move on to your own ideas.
good luck :)
i have code how player scores, as i said - the "if/something" part is exactly when player scores; so anything below "if/something" triggers every time when player succesfulle roll the ball to the center over and over
i have code how player scores, as i said - the "if/something/" part is exactly when player scores; so anything below "if/something/" triggers every time when player succesfully roll the ball to the center over and over
Your answer
Follow this Question
Related Questions
GUITime and Score issue. 0 Answers
Using if/else statements in GUI 1 Answer
How to create a GUI like Zombieville? 1 Answer