Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by MakeITeasy · Dec 13, 2013 at 03:27 PM · javascriptvariablesguitextpointshighscore

Score & High Score logic doesn't work

hello again!

i m making a memory card game. Every time i find a pair of cards i get points. When i can't find all the pairs in the game then a game over shows up and takes me to score scene. Now, in the score scene i have 2 GUIText objects on Hierarchy (points & high score) and 2 js files for them too.

I attach the points.js to points GUIText:

     public static var main_points:int;
     
     function Start () {
         if(Score_Manager_1.scoreFlag == true){
             guiText.text = "" + Score_Manager_1.points_;
             main_points = Score_Manager_1.points_;
         }
 }

And then i attach the highScore.js to high score GUIText:

 public var newHighScore:int;
 
     function Update () {
     newHighScore = Score_Manager.main_points;
     
         if(Score_Manager_1.points_ >= newHighScore ){
             
             guiText.text = "" + Score_Manager_1.points_;
         }
     }

The logic of it seems fine, but: Everytime i gain the first points i can see them both as my collected points (point GUIText) and as my new high score (high score GUIText) too. That's obvious at the first time cause there is no previous high score to compare it with. But then later when i play again my scene and gain less points this time, my points show the right points i collected but the high score GUIText shows the same with points GUIText. Normally high score would have displayed the last high score i made.

What's the problem here? Thanx in advance!

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 EX_Darius · Dec 14, 2013 at 01:12 AM

this is where I think you went wrong:

newHighScore = Score_Manager.main_points;

When writing this in your update it constantly gives "newHighScore" the value of score_manager.main_points which is Score_Manager_1.points_ .

you could try changing your update to this:

     function Update () {
  
         if(Score_Manager_1.points_ >= newHighScore ){
  
            guiText.text = "" + Score_Manager_1.points_;
            newHighScore = Score_Manager.main_points; // you could even change main_points to score_manager_1.points_ for effeciency
         }
     }

this way the highscore will ONLY be set if the points are higher than the newHighScore, instead of constantly updating the newHighScore no matter what points you have.

hope this helped!

EDIT: do realize your value get reset every time you reload the scene/game

Comment
Add comment · Show 3 · 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 MakeITeasy · Dec 14, 2013 at 09:01 AM 0
Share

Thanks for ur reply @EX_Darius But still i get the same results :(

EDIT: as for keeping my high score i did this (with no effect)on highScore.js:

     public var newHighScore:int;
     
     function Awake() {
             guiText.text = "" + PlayerPrefs.GetInt("newHighScore"); //Returns the value corresponding to key in the preference file if it exists
     }
     
     function Update () {
     
 if(Score_$$anonymous$$anager_1.points_ >= newHighScore ){
             guiText.text = "" + Score_$$anonymous$$anager_1.points_;
             newHighScore = Score_$$anonymous$$anager_1.points_;
             PlayerPrefs.SetInt("newHighScore",newHighScore); // Sets the value of the preference identified by key
             //PlayerPrefs.Save(); // Writes all modified preferences to disk
   
         }
     }
 
  
avatar image EX_Darius · Dec 14, 2013 at 12:07 PM 0
Share

hmm could be a conflict with using 1 virtual int for 2 external values. have you tried making a method that returns the values(making the values private) ?

avatar image MakeITeasy · Dec 14, 2013 at 01:26 PM 0
Share

Sorry i made a mistake while i was copying the if statement above, i made couple of examples but no result yet my if statement is (still):

 if(Score_$$anonymous$$anager_1.points_ >= newHighScore )

ins$$anonymous$$d of what i had before

avatar image
0

Answer by fafase · Dec 14, 2013 at 11:48 AM

You do not need to perform the action in the update, it sounds like a waste of resources.

You should use a method or a property (which is a method).

This is attached to a GameManager object

GameManagerScript.cs

 private var _highScore:int;
 private var _score:int;
 var highScoreGUI : GUIText; // Drag the GUIText here and below
 var scoreGUI : GUIText;
 public int Score
 {
    _score = value;
    scoreGUI.text = _score.ToString();
    if(_score >= _highScore){
        _highScore = _score;
        highScore.text = _highScore.ToString();
    }
 }

Now it depends on your logic for increasing the score. Considering you have your enemies getting destroyed:

 private var _script:GameManagerScript;
 var point:int; //Give a value via inspector
 function Start(){
     _script = GameObject.Find("GameManager").GetComponent(CamScript);
     if(_script == null)Debug.Log("No component found");
 }
 function OnCollisionEnter(col:Collider){
    if(col.gameObject.tag == "projectile"){
        _script.Score += point;
        Destroy(gameObject);
    }
 }

So this goes on the enemy, first it finds the camera and the script. When a collision occurs, the script checks if it is the projectile, then adds the values to Score and destroy the object. Score is the property that will do the job.

Now you should have a method on the game manager somewhere that should take care of when to end the scene. Do you have a list of enemies? Do you wait for a certain amount of enemies killed?

Considering the number of enemies:

 private var _count:int = 0;
 private var _maxEnemies:int = 50;

 public int Score
 {
    _score = value;
    scoreGUI.text = _score.ToString();
    _count++;
    if(_score >= _highScore){
        _highScore = _score;
        highScore.text = _highScore.ToString();
    }
    if(_count >=_maxEnemies)
    {
        PlayerPrefs.SetInt("HighScore", _highScore);
        Application.LoadLevel("NextLevel");
    }
 }
Comment
Add comment · Show 6 · 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 MakeITeasy · Dec 14, 2013 at 12:18 PM 0
Share

It's a good approach @fafase but it won't work for me. I have to play with two GUITexts as well.

avatar image fafase · Dec 14, 2013 at 02:39 PM 0
Share

I edited the script so that your GUITexts are taken care of.

avatar image MakeITeasy · Dec 14, 2013 at 04:56 PM 0
Share

i tested ur approach according to what i've got on my project and still no results :(

I attached this script on $$anonymous$$ainCamera (highScore scene):

     private var _highScore:int;
     private var _score:int;
     var highScoreGUI : GUIText;
     var scoreGUI : GUIText;
 
 
 function Awake(){
 highScoreGUI.text = "" + PlayerPrefs.GetInt("_highScore"); 
 
 }
 
 function Start () {
     _score = Score_$$anonymous$$anager_1.points_;
     scoreGUI.text = _score.ToString();
     if(_score >= _highScore){
         _highScore = _score;
         highScoreGUI.text = _highScore.ToString();
     }
     PlayerPrefs.SetInt("_highScore",_highScore);
 
 }
avatar image EX_Darius · Dec 15, 2013 at 04:22 AM 0
Share

youre resetting the highscore in your start function.. every time you start the game, it sets the playerpref _highscore to
the default value. remove that line of code.

avatar image MakeITeasy · Dec 15, 2013 at 08:57 AM 0
Share

nop still no results :(

with PlayerPrefs.SetInt("_highScore",_highScore); on my script i mean to save the last value of _highScore so i can generate it back to the scene when i load it

the above codes look so logically right i don't know why they don't work for me though :((

Show more comments

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display variables in GUIText? 1 Answer

using playerprefs to update a guitext and record highscores 1 Answer

How to set points back to zero when reload the scene? 2 Answers

How do I change the text of a gui image text 1 Answer

Reference other scrips in prefab 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