I know there’s a lot of stuff about this but I’m so retarded that I can’t make it happen.
The last piece of my puzzle, a 2d android game, is a scoring system, that is attached to a GUI text, because I want to fiddle with the settings and fonts from there.
I need a scoring system based on time since the level loaded or travelled distance
I need the highscore to be saved, stored between scenes and after game is quit.
I need the highscore to be displayed on the GUI just for a period of time until it disappears
Thank you very very much, I am so desperate about this I am willing to pay for the script or at least add the person to the credits and praise them before I go to bed for a few weeks.
For the time since the level loaded try this: http://docs.unity3d.com/Documentation/ScriptReference/Time-timeSinceLevelLoad.html
If you’re looking to have the score correlate with the time just make a public variable to store the time in (or store it in player prefs temporarily if you’re switching scenes) and when you want to display a score have a script do some math to calculate score out of the elapsed time and display it via gui text. Put a simple timer on the gui text to make it display for a short time and when that time ends make it remove the gui text and switch scenes or whatever you want it to do.
I can’t give you much advice on score correlating to distance traveled since I don’t know how the traveling occurs, but you could possibly measure the units from the starting position to the ending position if you store the transform of the player object before it ever starts traveling and compare it to its transform position when it stops traveling. If it’s a scroller and the player isn’t actually traveling any number of units you’ll have to find another way to measure it.
public class ScoreController : MonoBehaviour {
private float score; // Time.time is a float, not an int
// Initalization
void OnStart () {
// Retrieve the score
score = PlayerPrefs.GetFloat( "score", 0.0f ); // default score is 0.0f
}
// Display the score
void OnGUI () {
GUI.Label( new Rect( ... ), score.ToString() ); // Give new Rect() the position and size
}
// OnDestroy (ie. when the player quits)
void OnDestroy () {
// Save the score
PlayerPrefs.SetFloat( "score", Time.time );
}
}
Also, if you need the “score” variable to update real-time, you need to update it inside the Update() or FixedUpdate() method.
Well the score doesn’t show on the GUI, that’s what didn’t work as expected and I don’t know how to implement it. I want the script to try and access GUI Text from outside the script.
Then I suggest you look into more Unity3D tutorials, because you need to understand the basics before starting with the more advanced stuff.
In your example, I would assume that you have a Player GameObject of some sort. On that Player object you can add components. If you created a C# called ScoreController (or whatever) based on my code, and added that script (ref. “Add Component”) to the player, you would probably get closer to where you want to be.
A little effort goes a long way. You literally posted code that assigned Time.time to a variable and then said “how come this doesn’t display my highscore in a GUIText and save it?” Also - writing things like “DESPERATE” and “BEGGING” in your topic titles isn’t going to make people respond to you faster.
Have you done the tutorials in the Learn section?
Have you read the manual section on GUI scripting?
Have you read the API documentation for GUI and PlayerPrefs?