• 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
0
Question by DVFrance · Jul 05, 2012 at 02:18 PM · scorestoring

Score storing

I guys, I search for the same problem but I didn't found something ...

I made my first video game (yeaaaah :) ) and everything works well until I build an .exe.

I explain, when I build the game and try playing on an other PC, the storing system for my score doesn't seems to work. I thought it was because I'm using the free version of Unity and not the professional one ??? (because it works fine on my computer even with the same .exe)

Here is my code in .js :

     #pragma strict
 
 var youwin : GameObject;
 var gainPoints:float=0;
 var menuScore:GameObject;
 
 menuScore=GameObject.Find("scorePoints");
 menuScore.GetComponent(GUIText).text="Score:"+gainPoints;
 
 function Start () {
  Time.timeScale = 1;
  youwin=GameObject.Find("youwin");
  youwin.GetComponent(GUIText).pixelOffset.x = 4096;
  youwin.GetComponent(GUIText).text = "Score:"+gainPoints;
 }
 
 function Update () {
 
 }
 
 function applyPoints(points:float){
  gainPoints=gainPoints+points;
  menuScore.GetComponent(GUIText).text="Score:"+gainPoints;
 }
 
 function AddScore(name : String, score : int){
    var newScore : int;
    var newName : String;
    var oldScore : int;
    var oldName : String;
    var i: int;
    newScore = gainPoints;
    newName = name;
 
 print(name);
 print(gainPoints);
 
    for( i=0;i<10;i++){
 
       if(PlayerPrefs.HasKey(i+"HScore") && PlayerPrefs.GetInt(i+"HScore")<newScore){
         // new score is higher than the stored score
         oldScore = PlayerPrefs.GetInt(i+"HScore");
         oldName = PlayerPrefs.GetString(i+"HScoreName");
         PlayerPrefs.SetInt(i+"HScore",newScore);
         PlayerPrefs.SetString(i+"HScoreName",newName);
         newScore = oldScore;
         newName = oldName;
       }
    }
 }
 
 
 function UpdateHScore(){
  var Score1 : GameObject;
  Score1= GameObject.Find("menu_container/txt_score_1st-score");
  
  var Score2 : GameObject;
  Score2= GameObject.Find("menu_container/txt_score_2nd-score");
  
  var Score3 : GameObject;
  Score3= GameObject.Find("menu_container/txt_score_3rd-score");
  
  var Score4 : GameObject;
  Score4= GameObject.Find("menu_container/txt_score_4th-score");
  
  var Score5 : GameObject;
  Score5= GameObject.Find("menu_container/txt_score_5th-score");
  
  var Score6 : GameObject;
  Score6= GameObject.Find("menu_container/txt_score_6th-score");
  
  var Score7 : GameObject;
  Score7= GameObject.Find("menu_container/txt_score_7th-score");
  
  var Score8 : GameObject;
  Score8= GameObject.Find("menu_container/txt_score_8th-score");
  
  var Score9 : GameObject;
  Score9= GameObject.Find("menu_container/txt_score_9th-score");
  
  var Score10 : GameObject;
  Score10= GameObject.Find("menu_container/txt_score_10th-score");
  
  
  var Name1 : GameObject;
  Name1= GameObject.Find("menu_container/txt_score_1st-name");
  
  var Name2 : GameObject;
  Name2= GameObject.Find("menu_container/txt_score_2nd-name"); 
  
  var Name3 : GameObject;
  Name3= GameObject.Find("menu_container/txt_score_3rd-name");
  
  var Name4 : GameObject;
  Name4= GameObject.Find("menu_container/txt_score_4th-name");
  
  var Name5 : GameObject;
  Name5= GameObject.Find("menu_container/txt_score_5th-name");
  
  var Name6 : GameObject;
  Name6= GameObject.Find("menu_container/txt_score_6th-name");
  
  var Name7 : GameObject;
  Name7= GameObject.Find("menu_container/txt_score_7th-name"); 
  
  var Name8 : GameObject;
  Name8= GameObject.Find("menu_container/txt_score_8th-name");
  
  var Name9 : GameObject;
  Name9= GameObject.Find("menu_container/txt_score_9th-name");
  
  var Name10 : GameObject;
  Name10= GameObject.Find("menu_container/txt_score_10th-name");
  
  
  print("score");
 
 
     Score1.guiText.text = PlayerPrefs.GetInt("0HScore").ToString();
  Name1.guiText.text = PlayerPrefs.GetString("0HScoreName");
 
     Score2.guiText.text = PlayerPrefs.GetInt("1HScore").ToString();
  Name2.guiText.text = PlayerPrefs.GetString("1HScoreName");
 
     Score3.guiText.text = PlayerPrefs.GetInt("2HScore").ToString();
  Name3.guiText.text = PlayerPrefs.GetString("2HScoreName");
 
     Score4.guiText.text = PlayerPrefs.GetInt("3HScore").ToString();
  Name4.guiText.text = PlayerPrefs.GetString("3HScoreName");
 
     Score5.guiText.text = PlayerPrefs.GetInt("4HScore").ToString();
  Name5.guiText.text = PlayerPrefs.GetString("4HScoreName");
 
     Score6.guiText.text = PlayerPrefs.GetInt("5HScore").ToString();
  Name6.guiText.text = PlayerPrefs.GetString("5HScoreName");
 
     Score7.guiText.text = PlayerPrefs.GetInt("6HScore").ToString();
  Name7.guiText.text = PlayerPrefs.GetString("6HScoreName");
 
     Score8.guiText.text = PlayerPrefs.GetInt("7HScore").ToString();
  Name8.guiText.text = PlayerPrefs.GetString("7HScoreName");
 
     Score9.guiText.text = PlayerPrefs.GetInt("8HScore").ToString();
  Name9.guiText.text = PlayerPrefs.GetString("8HScoreName");
 
     Score10.guiText.text = PlayerPrefs.GetInt("9HScore").ToString();
  Name10.guiText.text = PlayerPrefs.GetString("9HScoreName");
 }


Here is the code from where the score values are sent:

 #pragma strict
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 var on : int = 0;
 var NameEditArea : String = "Enter your name";
 
 
 
 
 function OnGUI () {
 
  if (on == 1){
  
  
  var go = GameObject.Find("ScoreCount");
  var score : int;
  
  Screen.showCursor=true;
  Time.timeScale = 0;
      // Make a text field that modifies name.
     var centeredStyle = GUI.skin.GetStyle("Label");
      
 
     centeredStyle.alignment = TextAnchor.UpperLeft;
      NameEditArea = GUI.TextArea (Rect (Screen.width/2-100, Screen.height/2-25, 200, 50), NameEditArea, 200);
     
      //name = GUI.TextField (Rect (Screen.width/2-50, Screen.height/2-25, 200, 50), name, centeredStyle);
  
  if (Input.GetKey ("enter")){
         go.GetComponent(ScoreScript).AddScore(NameEditArea ,score);
         Application.LoadLevel("foraine_shooter_menu");
         
  }
  
  
  if (GUI.Button(Rect(Screen.width/2+100,Screen.height/2-25,150,50),"Enter")){
  go.GetComponent(ScoreScript).AddScore(NameEditArea ,score);
         Application.LoadLevel("foraine_shooter_menu");
         
  }
  
  
 
  }
 
 
 }
  

Does someone knows something about this ???

Thanks

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kurotatsu · Jul 07, 2012 at 02:45 AM

If you are using floats for your score I recommend using playerPrefs to save. and it will transfer them between machines.

I used them to save and load a players position in my game. This has a working example also.

Here.

hope it helps.

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 DVFrance · Jul 09, 2012 at 01:04 PM

Tks a lot Kurotatsu, I'm analysing this and I'll post something working well !

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 kurotatsu · Jul 13, 2012 at 03:34 AM 0
Share

I updated the script to a copy and paste solution, so it's actually functional now.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Carrying a score through to the next scene 2 Answers

Timer Score display on Game Over Scene? 1 Answer

i cant figure this out i have 2 scripts and a static variable i need the variable to add 1 everytime the enemy hits me. 1 Answer

How to center the score in screen? 1 Answer

Accessing Script From Other Script Causes Lag? 1 Answer


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