- Home /
Save Scores In Scene
Hi
I have a scene where I collect coins and would like to save scores.
I created an object called Scores and placed a script in it called Scores.js. Any suggestions?
//Scores.js var menuSkin : GUISkin; var Score = 0;
function Awake() { Score = 0; }
function OnGUI () { GUI.skin = menuSkin; text = "Score: "+Score; GUI.Box (Rect (300,10,322,50), ""); GUI.Label (Rect (330, 18, 100, 30), text); }
function AddScore() { Score++; }
//Script to update score when coin is touched var scriptName : Scores;
function OnTriggerEnter( other : Collider ) { if (other.tag == "Coin") { scriptName.AddScore();//Add score here
Destroy(other.gameObject);
}
}
Hi, I fixed the code formatting in your question. Another time, please format it yourself by selecting the code and click the Code button in the formatting toolbar. :)
hiii... I am new to unity.. I am a flash developer... but now a days it seems to be unity is BEST for using 3d effects, I am developing games for global or social purpose... but i am facing a lot of problems to save my scores.. so i am searching for the same... After R&D I found some samples and tutorials...... check these samples and tutorials, and tell me if you are familiar with all that. Guys if You know anyThing about my problem... then please let me know... thanxs... Gamers waiting for you reply.
Answer by KvanteTore · Mar 30, 2010 at 09:16 AM
You can use the PlayerPrefs
class to store values between sessions. If you only want to store it between levels, you can use the DontDestroyOnLoad
method.
This has been discussed a few times before: http://answers.unity3d.com/questions/3865/how-can-i-save-and-load-a-players-position, http://answers.unity3d.com/questions/5736/scene-to-scene-variables
Answer by Preetthefire · Jul 10, 2012 at 09:31 PM
player var Scores : float = 0.0; var skin : GUISkin;
function Start ()
{
//it will automotically load the last value.
Scores = PlayerPrefs.GetFloat("Coin");
}
function OnGUI ()
{
GUI.skin = skin;
GUI.Label (Rect(330, 18, 100, 30), "Scores: " + Scores);
}
function OnTriggerEnter (other:Collider)
{
if (other.tag == "Coin")
{
var coin = other.GetComponent(coinScript);
AddScore(coin.value);
Destroy(other.gameObject);
}
//it will save automatically on save points.
if (other.tag == "SavePoint")
{
//it will automatically check if this value is more then the previous
if (PlayerPrefs.GetFloat("Coin")<Scores)
{
PlayerPrefs.SetFloat("Coin", Scores);
}
}
}
function AddScore (value : float)
{
Scores += value;
}
coin
var value : float = 1.0; var animation : Animation;
.... bla bla bla i think that you know how to do.
Answer by x-dana · Feb 20, 2011 at 02:06 PM
Hi. I have the same problem. I want to save a game: position and score (a variable in a .js script). How can I do ? Tnx!!
Answer by Preetthefire · Jul 10, 2012 at 08:45 PM
player var Scores : float = 0.0; var skin : GUISkin;
function Start ()
{
//it will automotically load the last value.
Scores = PlayerPrefs.GetFloat("Coin");
}
function OnGUI ()
{
GUI.skin = skin;
GUI.Label (Rect(330, 18, 100, 30), "Scores: " + Scores);
}
function OnTriggerEnter (other:Collider)
{
if (other.tag == "Coin")
{
var coin = other.GetComponent(coinScript);
AddScore(coin.value);
Destroy(other.gameObject);
}
//it will save automatically on save points.
if (other.tag == "SavePoint")
{
//it will automatically check if this value is more then the previous
if (PlayerPrefs.GetFloat("Coin")<Scores)
{
PlayerPrefs.SetFloat("Coin", Scores);
}
}
}
function AddScore (value : float)
{
Scores += value;
}
coin var value : float = 1.0; var animation : Animation; .... bla bla bla i think that you know how to do.
I have some equerries: 1)what would come @ "Coin" (i.e under other.tag) 2)What would come @ tag "SavePoin" 3)It is also not adding the value to score board
Your answer
![](https://koobas.hobune.stream/wayback/20220612135117im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Save/Load Variable with playerprefs 1 Answer
Saving variables? 3 Answers
Transform Position? 1 Answer