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 CurtyB · May 21, 2015 at 09:25 AM · saveloadlevel

Saving score to next level?

Hey I was trying to get my score to keep as the player goes to the next level, I tried doing DoNotDestroy but when I do that it creates two of the Players in the next level, I've tried other examples but i wasn't sure how to link the Count to a save function.

 var CountText:UI.Text;
 var PickupText:UI.Text;
 
 function Start () {
     Time.timeScale = 0;
     count=0;
     pickups=0;
     CountText.text=("");
     PickupText.text=("");
     winText.text=("");
     SetCountText();
     SetPickupText();
 }
 
 function OnTriggerEnter(other : Collider) 
     {
         if (other.gameObject.tag == "Pickup")
         {
             other.gameObject.SetActive (false);
             count += 1;
             SetCountText();
 
         }else if(other.tag == "Exit" && pickups >=8)
             {
                 transform.position = Vector3(0, 0.5, 0);
                 Time.timeScale = 0;
                 Application.LoadLevel("Level_02");
                 
         }else if(other.gameObject.tag =="MusicPickup1")
             {
             other.gameObject.SetActive (false);
             sound1.mute = false;
             pickups += 1;
             count += 5;
             SetCountText();
             SetPickupText();
             MusicNote.SetActive (false);
             MusicNoteActive.SetActive (true);
         }
 
 function SetCountText ()
     {
     CountText.text = "Count: " + count.ToString ();
     if (count >=100)
     {
         winText.text=("You Win!");
         Exit.gameObject.SetActive (true);
     }
 }
 function SetPickupText ()
     {
     PickupText.text = "Pickups: " + pickups.ToString ();
     if (pickups >=8)
     {
         winText.text=("ALl DONE!");
         Exit.gameObject.SetActive (true);
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by zaid87 · May 21, 2015 at 10:13 AM

Which other example? Did you mean PlayerPrefs (http://docs.unity3d.com/ScriptReference/PlayerPrefs.html)? If not, maybe you can try that?

The reason why there's a 2 Players is because the last one was carry over and the new was created when the scene is reloaded. One way of handling that is to create a static instance of that class and in the Awake function checks if the instance is null or not and initialise it to "this" if it is and destroy the game object if it's not. Example:

 void Awake() {
         DontDestroyOnLoad (this);
 
         if (instance == null) {
             instance = this;        
         } else {
             DestroyObject(gameObject);
         }
 }

Although I wouldn't recommend this way due to personal taste that it's seems messy. Another way I can think of is to set the value to static, which should make the value to never change (just make sure when you're careful when resetting it so it wouldn't be reset unintentionally). The PlayerPref might be a cleaner way though (save it at the end of the level and load it again at the start of the next one).

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 mikelortega · May 21, 2015 at 10:15 AM

  • First, set your class with DontDestroyOnLoad.

  • Then, when you load the new scene, check on Awake if the GameObject already exists, if it does, destroy the object that is being created.

An example:

 public class YourClass : MonoBehaviour
 {
     void Awake()
     {
         if (FindObjectsOfType(typeof(YourClass)).Length > 1)
             DestroyImmediate(gameObject); // Found instance of script in scene... suicide!
     }
     
     void Start()
     {
         DontDestroyOnLoad(gameObject); // First time here, never destroy
     }
 }

I 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 JCprogrammer · May 21, 2015 at 11:01 AM

Its easy. you just need to implement a class, say we name it Scores, and then you set it as your players static property. here is an example to show you how its done in action:

 public class Player : MonoBehaviour {
     
     public static Scores scores;
     public static bool gameInitiated = false;
     void Start()
     {
 
         if (gameInitiated)
         {
             scores.score += 100;
         }
         else
         {
             scores = new Scores();
             scores.score = 100;
             gameInitiated = true;
         }
     }
     
     void Update () {
         Debug.Log(scores.score);
     }
 
     void OnGUI()
     {
         if (GUILayout.Button("Load Next Level"))
         {
             if (Application.loadedLevelName == "scene1")
                 Application.LoadLevel("scene2");
             else
                 Application.LoadLevel("scene1");
         }
     }
 }
 
 public class Scores
 {
     public int score;
     public int lives;
 }

 
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.

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

Level exporting (with video example) 1 Answer

Anyway to save which seen the player was on, then load that scene back from another one at a different time? 2 Answers

Score system Level Load 1 Answer

checkpoint level please 2 Answers

Application load advanced loading NO EXPERIENCE 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