• 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
Question by SUPPEAR · Mar 23, 2015 at 02:27 AM · android2dmobilescore

Scoring System between two different scene?

Hello. I am trying to figure out why the ScoreManager doesn't work for my game. I am using the Scripts that were provided by Unity Tutorials w$$anonymous$$ch were

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class ScoreManager : MonoBehaviour
 {
     public static int score;        // The player's score.
     
     
     Text text;                      // Reference to the Text component.
     
     
     void Awake ()
     {
         // Set up the reference.
         text = GetComponent <Text> ();
         
         // Reset the score.
         score = 0;
     }
     
     
     void Update ()
     {
         // Set the displayed text to be the word "Score" followed by the score value.
         text.text = "Score: " + score;
     }
 }


And of course to update the score t$$anonymous$$s

 using UnityEngine;
 using System.Collections;
 
 public class Expands : MonoBehaviour {
     
     public Transform parent;
     public float expandAmount = 2f;
     public float localPosition = 1f;
     public int scoreValue = 1;
 
     void OnMouseDown()
     {
         parent.localScale *=expandAmount;
         for(int i =0;i<parent.c$$anonymous$$ldCount;i++)
         {
             parent.GetC$$anonymous$$ld(i).localScale = parent.GetC$$anonymous$$ld(i).localScale/expandAmount;
             parent.GetC$$anonymous$$ld(i).localPosition += new Vector3(0, 0, -1f);//Added a '+'
             ScoreManager.score += scoreValue;
         }
     }
 }


When the game is finished, it takes you to a different scene w$$anonymous$$ch would be sort of like the Menu you see at the end of a mobile game ( The game is for Mobile and is 2D ) The problem I have is that the Score does not get updated and stays at zero. If anyone knows the solution, please do say. All help is appreciated.

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by revolute · Mar 23, 2015 at 02:35 AM

The easiest way to do t$$anonymous$$s is make static class.

For instance, one can define a class public static and all its variables static.

Comment

People who like this

0 Show 1 · 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 revolute · Mar 23, 2015 at 02:37 AM 0
Share

However, as this can only hold static variables and functions, you may need to split classes.

Static classes cannot and does not need to be attached to a gameobject. It will stay alive.

avatar image

Answer by Socrates · Mar 23, 2015 at 02:36 AM

For clarification: Does the score get updated w$$anonymous$$le the game is being played, then when you move to the menu you revert to zero score?

If so, are you using Object.DontDestroyOnLoad() on w$$anonymous$$chever object is holding a copy of your ScoreManager script?

Also make sure that you are not creating a new instance of ScoreManager in the new menu scene. Your Awake() function is resetting the score to zero, so every time you create a new instance of t$$anonymous$$s class, you are resetting your score to zero.

Comment

People who like this

0 Show 0 · 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.

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

23 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

Related Questions

Why won't the Score reset? 2 Answers

Scene keeps reloading, can't keep score 3 Answers

Problem creating a script to destroy object on collision. 1 Answer

Score system 1 Answer

Collision On Object Error. Please help. 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