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 mrjiggyfly24 · Sep 03, 2014 at 09:47 AM · c#playerprefshighscores

Saving final score and displaying on main menu

Ok, so I've been trying for a while now and I can't seem to figure out how to save the score that the player has once they die and set that to the PlayerPrefs. The way my script is set up now it saves the int score which is "0" as the high score. Here's my script for the main menu, player, and generate.

using UnityEngine;

using System.Collections;

public class MainMenu : MonoBehaviour{

 public Texture backgroundTexture;
 
 public float guiPlacementY1;
 public float guiPlacementY2;
 public float guiPlacementY3;
 public float guiPlacementX1;
 public float guiPlacementX2;
 public float guiPlacementX3;
     
     void OnGUI()
     {    
         GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), backgroundTexture);
     
             if (GUI.Button (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), "Play Game")){
                 Application.LoadLevel("RunMouseRun");
             }    
             if (GUI.Button (new Rect(Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .5f, Screen.height * .1f), "Options")){
                 print ("Clicked Options");
             }    
             if (GUI.Button (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .1f), "Play Game")){
                 print ("Clicked Play Game");
             }    
             if (GUI.Button (new Rect(Screen.width * guiPlacementX2, Screen.height * guiPlacementY2, Screen.width * .5f, Screen.height * .1f), "Options")){
                 print ("Clicked Options");
             }
         GUI.Label(new Rect(Screen.width * guiPlacementX3, Screen.height * guiPlacementY3, 300, 50), "High Score: " +PlayerPrefs.GetInt("HighScore"));
     }

}

using UnityEngine;

public class Player : MonoBehaviour{

 void Update (){
 
 Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
 if (screenPosition.x > Screen.width || screenPosition.x < 0)
     {
         Die();
     }
 }
 void OnCollisionEnter2D(Collision2D other)
 {
     Die();
 }
 
 void Die()
 {
     Application.LoadLevel("MainMenu");
 }

}

using UnityEngine;

public class Generate : MonoBehaviour{

 public GameObject Walls;
 public GameObject Ground;
 public GameObject Fence;
 int score = 0;
 
 // Use this for initialization
 void Start()
 {
     //PlayerPrefs.SetInt("HighScore", score);
     StoreHighScore();
     PlayerPrefs.Save();
     InvokeRepeating("CreateObstacle", 4f,4f);
     InvokeRepeating("CreateGround", .1f,6.8f);
     InvokeRepeating("CreateObstacle1", 2f,4f); 
 }    
 // Update is called once per frame
 void OnGUI ()
 {
     GUI.color = Color.black;
     GUILayout.Label("Score:" + score.ToString());
 }
 
 void CreateObstacle()
 {
     Instantiate(Walls);
     score++;
 }
 void CreateGround()
 {
     Instantiate(Ground);
 }
 void CreateObstacle1()
 {
     Instantiate(Fence);
     score++;
 }
 void StoreHighScore()
 {
     if (score > PlayerPrefs.GetInt("HighScore"))
         PlayerPrefs.SetInt("HighScore", score);
 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by froYo · Sep 03, 2014 at 10:49 AM

Is there any reason why your "StoreHighScore" function is being called in the "Start" function of the "Generate" class? This means that it's only ever going to attempt to set your high score at the initialization of the object with the "Generate" script attached.

From what i can see, to get this to work, you simply need to have a reference to the "Generate" class in your "Player" class and then call "StoreHighScore". BEFORE you load the "Main Menu" in the player class "Die" function.

Hope this helps.

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 mrjiggyfly24 · Sep 03, 2014 at 07:23 PM 0
Share

I am getting an error saying that getHighScore isn't in the current context. $$anonymous$$y variable of score is in generate so I can't move the algorithm to deter$$anonymous$$e final score to the player.cs file, but visa verse for the getHighScore in the generate.cs file.... any advice?

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

How To Add PlayerPrefs Scores? 1 Answer

How to check if string array contains duplicates C# 1 Answer

Finding Distance between Angles and Points 2 Answers

C# Deserializing XML and Assigning Values 1 Answer

How to create an Idle counter ? 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