Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by cmos · Mar 30, 2010 at 08:49 AM · variablesavepersist

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);
 }

}

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image runevision ♦♦ · Mar 30, 2010 at 12:20 PM 0
Share

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. :)

avatar image akkiDev · Sep 17, 2013 at 06:43 PM 0
Share

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.

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image himanshu619 · Dec 15, 2012 at 08:07 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Save/Load Variable with playerprefs 1 Answer

Saving variables? 3 Answers

Transform Position? 1 Answer

Save what you write in textarea as variable? 1 Answer

Checking if int is !null not working ? Strange... 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges