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
2
Question by BCouto · Oct 29, 2010 at 05:03 AM · scenescore

Keeping a score from one scene to another?

My game is keeping track of a high score. I want this score to display when the scene changes to the win Screen. I'm a bit confused as to how to go about doing this. I have the DontDestroyOnLoad line in my script, but how would I go about actually calling the variable into my new scene? Since it's a new scene, the script doesn't actually exist in that scene yet. Would I have to copy and paste the script into the assets folder in the new scene or would I have to call if through code, and if so, how?

Thanks for the help.

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 zannghast · Oct 29, 2010 at 05:25 AM 0
Share

Hi BCouto, correct me if I'm wrong, where and how did you call DontDestroyOnLoad? I'm assu$$anonymous$$g you attached it to a gameobject that 'keeps track' of the score. If the method call was successful, you should be able to reference that script attached to the gameObject from any script existing in the new scene.

Also, you may want to check out this similar question that was answered some months back. $$anonymous$$ay give you another option for setting level stats throughout your game.

avatar image zannghast · Oct 29, 2010 at 05:26 AM 0
Share

Sorry, forgot to add the link: http://answers.unity3d.com/questions/9996/singleton-score-keeping-and-ongui-updates

2 Replies

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

Answer by Lab013 · Mar 18, 2011 at 06:36 AM

Just to make sure you're doing this:

function Start() {
   DontDestroyOnLoad(gameObject);
}

What that does, is whenever you load a new level, the object (and its scripts) won't be destroyed.

There are other ways to do this though. You could have a script like this:

// I'm going to pretend this scripts name is MyScript static var score : int = 100;

function OnGUI() { GUILayout.Label("Your Score: " + score);

}

Then in another script you could do something like this

function KillEnemy() {
++MyScript.score;
} // Award the player 10 points

Because score is a static variable, there is only one instance of that particular variable. You don't need a class instance to access that variable, it can be accessed from anywhere just by using the ClassName.variable syntax.

Comment
Add comment · Show 3 · 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 BeeVee · Mar 19, 2013 at 09:09 PM 0
Share

Thanks Lab013, just the answer I needed! (I don't seem to be able to upvote your response?)

B

avatar image sailesh · Apr 24, 2014 at 07:51 PM 0
Share

Is it fine if I save that another script from other scene?

avatar image athir · Mar 08, 2017 at 07:21 PM 0
Share

here is my problem " DontDestroyOnLoad(gameObject); " is working fine...but i set my game that my player only have 1 live and return to 1st scene when he die...whenever this happen...my score didn't reset itself back to 0...it only gains more and more points...how do i reset this when my player die and still keep the high score? do i need to do this in different script? score script and high score script? well that's a thought...hmmm...but not entirely sure...

avatar image
0

Answer by Atnas1010 · Oct 29, 2010 at 05:12 AM

You could do something like this:

public var score : float;

function OnGUI() { if (Application.loadedLevelName == "WinScene") GUILayout.Label(score.ToString()); }

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Restarting game with last score from playerprefs. PLEASE HELP!! 1 Answer

Show Current Score on GAMEOVER Scene 0 Answers

Level end after certian amount of points collected 2 Answers

How do I create multiple save files and make it work? 2 Answers

Final score in new scene 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