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 BenAlkaline · Jul 05, 2013 at 05:39 AM · playerprefsscore

Where am I going wrong with these scripts?

Hello I'm trying to create a basic game in which the player runs through a level and is penalised each time they hit an object. Whenever they hit an object their score goes up by 1. Here's the script I'm using for scoring:

 var score = 0;
 var scoreText = "Number of hits: ";
 var mySkin : GUISkin;
 var saveScoreScript : RecordScore;
 
 function Start(){
 saveScoreScript = GetComponent(RecordScore);
 }
 
 function OnTriggerEnter( other : Collider ) {
     Debug.Log("OnTriggerEnter() was called");
 
         if (other.tag == "obstacle1") {
               Debug.Log("Other object is obstacle1");
               score += 1; 
 
      }
         else if (other.tag == "obstacle2") {
         Debug.Log("Other object is obstacle2");
         score = Mathf.Max(0, score + 1);
      }
         if (other.tag == "obstacle3" && score > 0) {
         Debug.Log("Other object is obstacle3");
         score = Mathf.Max(0, score + 1);
     }
     
     scoreText = "Score: " + score;
     Debug.Log("Number of hits: " + score);
     saveScoreScript.AddScore(score);
     
 }
 
 function OnGUI () {
     GUI.skin = mySkin;
     GUI.Box (Rect (140, 10, 500, 200), scoreText.ToString());
  
      }

I'm saving the player's score, if it's lower than their previous, with this script I've named RecordScore (as the line var saveScoreScript : RecordScore; shows):

 function AddScore(score : int){
          if(PlayerPrefs.GetInt("NumberOfHits") >  score)
             PlayerPrefs.SetInt("NumberOfHits",score);
   }

And I'm using this script to display the player's best score, basically the lowest amount of times they've hit an object, next to their current score in the same scene:

 var LowestNumberOfHitsAchieved : int;
 var mySkin : GUISkin;
 var scoreText = "Lowest Number of Hits: ";
 
 function Update(){    
     LowestNumberOfHitsAchieved = PlayerPrefs.GetInt("NumberOfHits");
     }
     scoreText  = "Lowest Number of Hits: " + PlayerPrefs.GetInt("NumberOfHits");
 
 function OnGUI () {
     GUI.skin = mySkin;
     GUI.Box (Rect (445, 5, 500, 200), scoreText.ToString());
      }

The only thing is that the player's best score (Lowest Number of Hits: ) isn't being updated , it constantly reads as 0 I think because 0 is the lowest score the player can have. Let's say the player runs through the level for the first time and achieves a score of 30. The second time they play their best score should read as 30. As they play the level a second time they achieve a score of 15. So the third time they open up the level their best score should read as 15 (if that doesn't sound too long winded). But like I said the problem is that their best score isn't being updated each time they play, it still reads as 0. I'm not sure where to go from here.

Thank you kindly for any answers or feedback, -Ben.

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 Jamora · Jul 05, 2013 at 06:55 AM 0
Share

You asked for feedback, so here's my two cents.

Do you really need different behavior for different objects being bumped into? If not, just add one to score every OnTriggerEnter(). If you have only one obstacle that needs special attention (obstacle3), what you're doing is good enough. Otherwise, consider creating an interface, ICollidable or whatever so you can define the needed behavior in the object being collided into, ins$$anonymous$$d of your scoring script. This will lessen the chance of you forgetting to add a tag to an obstacle and getting frustrated while hunting the bug...

avatar image BenAlkaline · Jul 06, 2013 at 12:44 AM 0
Share

Thank you very much, I'll change it up like you said. Coding takes some getting used to.

1 Reply

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

Answer by D4rt · Jul 05, 2013 at 06:10 AM

You do not initialize the "NumberOfHits" anywhere. You can either

A. Set the "NumberOfHits" to something very big.

or

B. Test if the PlayerPrefs doesn't have the key in which case we add the score in.

 function AddScore(score : int)
 {
      if(PlayerPrefs.GetInt("NumberOfHits") >  score || !PlayerPrefs.HasKey("NumberOfHits"))
         PlayerPrefs.SetInt("NumberOfHits",score);
 }

Also this:

 function Update(){  
     LowestNumberOfHitsAchieved = PlayerPrefs.GetInt("NumberOfHits");
     }
     scoreText  = "Lowest Number of Hits: " + PlayerPrefs.GetInt("NumberOfHits");

Should probably be like this:

 function Update(){  
     LowestNumberOfHitsAchieved = PlayerPrefs.GetInt("NumberOfHits");
     scoreText  = "Lowest Number of Hits: " + LowestNumberOfHitsAchieved;
     }

EDIT: One more thing. saveScoreScript.AddScore(score) should probably be called only after the game is over. Now you add the score every time a collision happens so at first you add a score of 1 after which it's the "best" score.

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 BenAlkaline · Jul 06, 2013 at 12:43 AM 0
Share

Thank you very much, everything's working fine 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.

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

Problem with the Playerprefs 1 Answer

Playerprefs not work!? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Is it necessary to create an Array() to show high scores in PlayerPref? 1 Answer

update highscore if current is higher than previous score 0 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