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
1
Question by himanshu619 · Dec 15, 2012 at 09:50 PM · scoreloadlevelscore system

Save/Add score while different level load

I have the following script, for my score card. The issue, I'm facing is when the next level(scene) is loaded the score starts from 0. Please fix the code for me so that it save the score of current scene and add the next level score into it whenever next level is loaded! Note: I had used Do not destroy but it haven't worked for me! Script: var score = 0; var scoreText = "Score: 0"; var mySkin : GUISkin;

function OnTriggerEnter(other : Collider ) {

if (other.tag == "Pickup") { //Pickup is the tag for coin

     Debug.Log("Other object is a coin");

     score += 1;         //Score integer 

     scoreText = "Score: " + score;

     Debug.Log("Score is now " + score);

     Destroy(other.gameObject);

 }

}

function OnGUI () {

 GUI.skin = mySkin;

 GUI.Label (Rect (200, 10, 200, 400), scoreText.ToString()); 

}

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

Answer by clunk47 · Dec 17, 2012 at 02:25 AM

Make the score variable static.

EDIT: DoNotDestroyOnLoad not needed if using static. (Special thanks @fafase)

Comment
Add comment · Show 11 · 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 clunk47 · Dec 17, 2012 at 02:52 AM 2
Share

If I have to write a script for you it will be a a while because I'm working, but I will get to it later if you dont' figure it out.

avatar image himanshu619 · Dec 17, 2012 at 04:20 AM 1
Share

Thanks for your kind response. I'll try again at my end and also please try to send my your script too when you get time! Thanks once again!

avatar image himanshu619 · Dec 17, 2012 at 03:25 PM 1
Share

Thanks clunk it's working now for me after adding static. I had not used "destroy..." because it was creating multiple players on each level. Thanks once again!

avatar image clunk47 · Dec 17, 2012 at 05:43 PM 1
Share

No problem Sir, thank you for asking a good question, I have voted +1 :)

avatar image fafase · Dec 17, 2012 at 06:31 PM 1
Share

You do not need DontDestroyOnLoad if you use static since when you store the value in the static they won't be destroyed anyway. They actually provide an easy way to store and retrieve information and they are actually faster than a normal instance variable.

What I would recommend is to keep your script as it is but create a static class. At the end of the level before loading the next scene, transfer your values to the static class. You can then retrieve them on the other side.

Using static all the way can be "unfair". If you restart the same level, the score is kept and add each time you restart, if you use a normal variable it is set back to 0 if you reload.

See my point?

Show more comments

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

11 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

Related Questions

how can i load a scene when reaching certain score? Roll-a-Ball Project 1 Answer

How to check if a car has perform stunt? 0 Answers

Can't increase my score after OnTriggerEnter occurs. 1 Answer

Scoring System 3 Answers

How to add score after destroying object in unity 2D? 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