• 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 iOSdev747 · Jun 23, 2017 at 10:54 AM · unity 5textgamelabelhighscores

help me to save high score??

how should I save highest no of coins which loads when game starts again my code is using UnityEngine; using System.Collections;

public class MouseController : MonoBehaviour {

 public float jetpackForce = 75.0f;

 public float forwardMovementSpeed = 3.0f;

 public Transform groundCheckTransform;
 
 private bool grounded;
 
 public LayerMask groundCheckLayerMask;
 
 Animator animator;

 public ParticleSystem jetpack;

 private bool dead = false;

 private uint coins = 0;

 public Texture2D coinIconTexture;

 public AudioClip coinCollectSound;

 public AudioSource jetpackAudio;
 
 public AudioSource footstepsAudio;

 public ParallaxScroll parallax;

 // Use this for initialization
 void Start () {
     animator = GetComponent<Animator>();    
 }
 
 // Update is called once per frame
 void Update () {
 
 }

 void FixedUpdate () 
 {
     bool jetpackActive = Input.GetButton("Fire1");
     
     jetpackActive = jetpackActive && !dead;
     
     if (jetpackActive)
     {
         GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jetpackForce));
     }
     
     if (!dead)
     {
         Vector2 newVelocity = GetComponent<Rigidbody2D>().velocity;
         newVelocity.x = forwardMovementSpeed;
         GetComponent<Rigidbody2D>().velocity = newVelocity;
     }
     
     UpdateGroundedStatus();
     
     AdjustJetpack(jetpackActive);

     AdjustFootstepsAndJetpackSound(jetpackActive);

     parallax.offset = transform.position.x;
 } 

 void UpdateGroundedStatus()
 {
     //1
     grounded = Physics2D.OverlapCircle(groundCheckTransform.position, 0.1f, groundCheckLayerMask);
     
     //2
     animator.SetBool("grounded", grounded);
 }

 void AdjustJetpack (bool jetpackActive)
 {
     jetpack.enableEmission = !grounded;
     jetpack.emissionRate = jetpackActive ? 300.0f : 75.0f; 
 }

 void OnTriggerEnter2D(Collider2D collider)
 {
     if (collider.gameObject.CompareTag("Coins"))
         CollectCoin(collider);
     else
         HitByLaser(collider);
 }
     
 void HitByLaser(Collider2D laserCollider)
 {
     if (!dead)
         laserCollider.gameObject.GetComponent<AudioSource>().Play();

     dead = true;

     animator.SetBool("dead", true);
 }

 void CollectCoin(Collider2D coinCollider)
 {
     coins++;
     
     Destroy(coinCollider.gameObject);

     AudioSource.PlayClipAtPoint(coinCollectSound, transform.position);
 }

 void OnGUI()
 {
     DisplayCoinsCount();

     DisplayRestartButton();
 }

 void DisplayCoinsCount()
 {
     Rect coinIconRect = new Rect(10, 10, 32, 32);
     GUI.DrawTexture(coinIconRect, coinIconTexture);                         
     
     GUIStyle style = new GUIStyle();
     style.fontSize = 30;
     style.fontStyle = FontStyle.Bold;
     style.normal.textColor = Color.yellow;
     
     Rect labelRect = new Rect(coinIconRect.xMax, coinIconRect.y, 60, 32);
     GUI.Label(labelRect, coins.ToString(), style);
 }

 void DisplayRestartButton()
 {
     if (dead && grounded)
     {
         Rect buttonRect = new Rect(Screen.width * 0.35f, Screen.height * 0.45f, Screen.width * 0.30f, Screen.height * 0.1f);
         if (GUI.Button(buttonRect, "Tap to restart!"))
         {
             Application.LoadLevel (Application.loadedLevelName);
         };
     }
 }

 void AdjustFootstepsAndJetpackSound(bool jetpackActive)    
 {
     footstepsAudio.enabled = !dead && grounded;
     
     jetpackAudio.enabled =  !dead && !grounded;
     jetpackAudio.volume = jetpackActive ? 1.0f : 0.5f;        
 }

}

help me to add the statements to save no. of coins which loads at startup I am new to scripts

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 MaishyN · Jun 23, 2017 at 11:40 AM

PlayerPrefs is one of the possible solutions and probably the easiest way to go. Just take a quick look at this: https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

In code it would be something like:

 //setting max coins
 if (coins > PlayerPrefs.GetInt("MaxCoins"))
     PlayerPrefs.SetInt("MaxCoins", coins);
 //getting max coins
 someInt = PlayerPrefs.GetInt("MaxCoins");

It should give you a hint where to start.

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 FlaSh-G · Jun 23, 2017 at 11:44 AM 0
Share

Eh, typed too slowly :)

avatar image
0

Answer by FlaSh-G · Jun 23, 2017 at 11:44 AM

Use PlayerPrefs. They're fairly easy to use:

 public void GameEnded()
 {
   if(coins > PlayerPrefs.GetInt("coins"))
   {
     PlayerPrefs.SetInt("coins", coins);
     Debug.Log("Saved new Highscore!");
   }
 }
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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Need help in typing project 0 Answers

The variable has not been assigned error ? 0 Answers

Color.white not so much white... 3 Answers

My code will not change Canvas Text at all [No errors] 3 Answers

Inputfield text to String variable 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges