• 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 bearded_pete · Apr 01, 2014 at 06:21 PM · savescore

Carrying a score through to the next scene

I know this is a prevalent question here on the Unity forums but I am hoping that my situation is different enough to warrant a question. I want to save the score the player received in one scene and display it in the next while adding on what they earn in the following scene. Here is the code I am working with: using UnityEngine; using System.Collections;

public class Deactivate : MonoBehaviour {

 public GUIText countText;
 public GUIText winText;
 public GUIText startText;
 public GUIText coinText;
 public float delayTime = 3;
 private int count;
 private int count2;


 void Start ()
 {
     countText.text = "Score: 0";
     coinText.text = "Gold: 0";
     startText.text = "";
     SetCountText ();
     winText.text = "";
 }

 void OnTriggerEnter(Collider other) 
 {
     if(other.gameObject.tag == "PickUp")
     {
         other.gameObject.SetActive(false); 
         count = count + 100;
         count2 = count2 + 1;
         SetCountText ();
         audio.Play ();
     }
     if(other.gameObject.tag == "PickUp2")
     {
         other.gameObject.SetActive(false); 
         count = count + 500;
         count2 = count2 + 5;
         SetCountText ();
         audio.Play ();
     }
     if(other.gameObject.tag == "Enemy")
     {
         other.gameObject.SetActive(false); 
         count = count + 50;
         SetCountText ();
         audio.Play ();
     }
     if(other.gameObject.tag == "Boss")
     {
         other.gameObject.SetActive(false); 
         count = count + 5000;
         SetCountText ();
         audio.Play ();
     }
 }
 void SetCoinText ()
 {
     coinText.text = "Gold: " + 0 + count2.ToString ();
 }

}

I tried the DonotDestroyOnLoad method and it doubled up the code after a death and killed the player when it collected a coin. I hope there is a simple solution to this problem and and grateful for any help I receive. Thanks.

Comment
Add comment · Show 1
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 g0tNoodles · Apr 01, 2014 at 07:54 PM 0
Share

A potential solution to your issue would be to save the score in the player prefs and then load it in each scene.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tol'Satha · Apr 01, 2014 at 08:07 PM

The reason it doubled up is that you are loading a scene which already has that script, while keeping this one from the last scene. An easy way to prevent this is to run a search at Start() for the gameobject containing this script, and if it finds one, it destroys itself. That way, the 2nd script will delete itself leaving only the original one from the first scene.

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 POLYGAMe · Apr 01, 2014 at 08:05 PM

I usually just use static variables for my scores. They maintain their value and can be accessed from any script. You can use PlayerPrefs too but a static int or float will do the trick ;)

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 senc01a · Apr 02, 2014 at 03:00 PM 1
Share

I (as well as many others) strongly recommend not to use this technique for too many reasons. Check out the Singleton Pattern, and how to make objects remain alive from once scene to another.

avatar image POLYGAMe · Apr 04, 2014 at 01:06 AM 0
Share

I'm not sure about the OP's background but most Unity devs aren't "programmers", they are designers, using a tool that makes it possible to create games, without being a C++ genius.

Sometimes things that are considered "against protocol" are simple and easy ways to just make stuff work, which, at the end of the day, is most important.

I'm not suggesting people shouldn't try and learn more but in the case of a simple score variable, I see nothing wrong with using a simple global variable in your main controller script.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

PlayerPrefs 2 Answers

PlayerPrefs HighScore problems, it doesn't work. 4 Answers

Save high score and coins to Space Shooter game 1 Answer

Save and Display Best Score 2 Answers

How to save High Scores 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