• 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 SUPPEAR · Apr 06, 2015 at 06:52 PM · android2dgameobjecttextscore

Carry over texts from one scene to another?

Hello. I am making a game that is 2D and for Android. I recently made a post asking how to set a score to zero using a button. Before the post, I made the score update every second and the text would carry over. Now the score is only updated every time you score a point, which is good, and pressing the button does make it to zero. The score does transfer over, but the text doesn't actually show it. What I mean is that it knows how many points are scored but the text just says New Text instead of saying Score: 24 ( For example ) What needs to be done for it to show the score instead of saying New Text?

The scoremanager script is

 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 ()
     {
         text = GetComponent <Text> ();
     }
     
     public void SetScore (int newScore)
     {
         ScoreManager.score = newScore; //update the static score variable
         text.text = "Score: " + score;
     }
 }

and the script that actually detects for a point to be added is

 using UnityEngine;
 using System.Collections;
 
 public class Score : MonoBehaviour {
     public int scoreValue = 1;
     public ScoreManager scoreManager;
     
 
     
     void OnMouseDown () {
         scoreManager.SetScore(ScoreManager.score + scoreValue);
     }
 }

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 UNZoOM · Apr 06, 2015 at 07:12 PM 0
Share

so when you debug newScore after line 17 , do you get the updated "new score" ?

avatar image SUPPEAR · Apr 06, 2015 at 09:01 PM 0
Share

Yes I do. It's just that the script I am using only updates when a point is scored. Which means the text won't update when the scene changes. Unless somehow a point was added then it would show the score.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Digital-Phantom · Apr 06, 2015 at 07:16 PM

Just use DoNotDestroyOnLoad to carry the scoremanager object over from scene to scene.

Comment
Add comment · 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 SUPPEAR · Apr 06, 2015 at 08:43 PM 0
Share

Okay... I've done that. That doesn't change anything. I need to figure out a way for it to update the text to display the score. Since the text will only update if the score changes.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Score system 1 Answer

OnLevelWasLoaded script problem. 1 Answer

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

Transform.localPosition Script Problem. 3 Answers

Displaying the score after a reset. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges