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 ThatBenGuy · Jun 22, 2012 at 10:05 AM · scenescore

Loading a scene and keeping original score

Hello I'm trying to load from one scene to another while keeping the original score. The game I'm working on involves collecting coins to add to a score, over multiple scenes. I wish to go from 'Scene 1' to 'Scene 2' while keeping the score accumulated in 'Scene 1' to be used and added to in 'Scene 2'.

If it helps the script I'm using for the scoring system is:

     var score = 0;
     var scoreText = "Score: 0";
     var mySkin : GUISkin;
     
     function OnTriggerEnter(other : Collider ) {
     
        if (other.tag == "Coin") {
     
             Debug.Log("Other object is a coin");
     
             score += 140;
     
             scoreText = "Score: " + score;
     
             Debug.Log("Score is now " + score);
     
             Destroy(other.gameObject);
     
         }
     
         else if (other.tag == "Rock" && score > 0) {
     
         Debug.Log("Other object is a rock");
     
         score += -100;
     
         scoreText = "Score: " + score;
     
         Debug.Log("Score is now " + score);
     
         }
     
     
     
         else if (other.tag == "Tree" && score > 0) {
     
         Debug.Log("Other object is a tree");
     
         score += -90;
     
         scoreText = "Score: " + score;
     
         Debug.Log("Score is now " + score);
     
         }
     
     }
     
     
     
     function OnGUI () {
     
         GUI.skin = mySkin;
     
         GUI.Label (Rect (250, 10, 200, 400), scoreText.ToString()); 



To transition from scene to scene I'm using this script, attached to a 'Barrier':

  function OnTriggerEnter ()
     
     {
     
     Application.LoadLevel ("Scene 2");
     
     }

I've tried using a DontDestroyOnLoad script which works to a certain extent. It keeps the score, and after collecting coins in 'Scene 2' they add to the original score. However, upon activating the scene-transition script mentioned earlier the next scene loads with the 'Player' starting directly after it collided with the 'Barrier', which activates the scene-transition script. This is a bit of a problem as the 'Player' needs to start from the very beginning of 'Scene 2'. Without the DontDestroyOnLoad script the 'Player' does start at the very beginning of the next scene but the score isn't intact.

I apoligise for the long question. Thank you for any answers or feedback, -Ben

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
Best Answer

Answer by kurotatsu · Jul 13, 2012 at 03:39 AM

Updated the script as a copy and paste solution, so now it's actually functional.

HERE

Comment
Add comment · Show 2 · 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 ThatBenGuy · Jul 23, 2012 at 04:45 AM 0
Share

Thank you very much :)

avatar image kurotatsu · Jul 23, 2012 at 11:02 PM 0
Share

You are most welcome.

avatar image
0

Answer by whydoidoit · Jun 22, 2012 at 10:16 AM

You can make a separate object - an empty game object - and put your do not destroy script on that. Then make that object a singleton:

  static var Instance : YourScriptName;
  var score : int;

  function Awake()
  {
        Instance = this;
        DontDestroyOnLoad(this);
  }

Now you can access your score like this:

   YourScriptName.Instance.score += 100;
Comment
Add comment · Show 2 · 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 ThatBenGuy · Jun 23, 2012 at 04:25 AM 0
Share

Thank you very much for your answer. However when I make a script for an empty game object, to turn that into a singleton it doesn't work. I named the singleton script '$$anonymous$$eepScore', In the first line of that script: 'static var Instance : YourScriptName;' I changed it to: static var Instance : ScoreSystem;. 'ScoreSystem' being the name for my scoring system script. When I try and attach the script to an empty game object it comes up with an error saying 'Cannot convert $$anonymous$$eepScore to ScoreSystem'. Also with the second code you provided 'YourScriptName.Instance.score += 100;' do I make that a separate script altogether and attach it to my empty game object?

avatar image Cloaking_Ocean · Nov 05, 2014 at 05:22 PM 0
Share

In response to whydoidoit, Your making a static reference of the script in which you're writing this code. So you created a script called $$anonymous$$eepScore which is the one you're putting this code into. You're making a static reference of the $$anonymous$$eepScore script. You can change the values such as score in your scene with other scripts by doing $$anonymous$$eepScore.score, but you'd have to make score static.

avatar image
0

Answer by kurotatsu · Jun 23, 2012 at 09:03 AM

I did it via learning about player preferences here: http://answers.unity3d.com/questions/250734/player-prefs-saving-position-script-assistance.html

There are other methods as well, but I used player prefs to also save player profiles, and carry them over to other scenes too. it's incredibly easy to save an integer using player prefs. Also refer to function Onlevelwasloaded() or in your function Awake to load your score in the next screen from your player pref save.

Hope this 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

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

Final score in new scene 1 Answer

Problem with the Playerprefs 1 Answer

Script refering to more than one GameObject 1 Answer

Same Script in a different Scene with different conditions 1 Answer

Accessing non-static functions in an unattached, non-scene, "project" script 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