Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DaProtato · Jan 02, 2018 at 07:28 PM · uiplayerprefsscene-switchingdontdestroyonloadscore system

How do you transfer UI Text from one scene to another?

The scoring of my game is based on how far the player travels on the x-axis. I use this code to calculate it:

 public Transform player;
 public Text scoreText;

 void Update()
 {

     scoreText.text = player.position.x.ToString("0");

 }


After the player collides with an object, they are sent to the Game Over scene. The problem is, I am not able to/ I don't know how to display the final score when they lose. I have tried using DontDestroyOnLoad and PlayerPrefs, but I couldn't get either of them to work with UI Text.

Can someone help me?

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 hexagonius · Jan 02, 2018 at 07:46 PM 0
Share

You're not really clear on what you wanted to keep and how you tried to do it. DontDestroyOnLoad is the right thing to keep gameobjects between scenes and PlayerPrefs is a valid way to transfer data. Since neither of the ways described are shown in your code nor do you explain how exactly you tried to use them, it's hard to help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by leSamo · Jan 02, 2018 at 08:41 PM

Why don't you just store the text string and than set the next scene's UI text.text to that string Method 1:

 // Old scene
 string score = player.position.x.ToString("0");
 PlayerPrefs.SetString("score", score);
 
 // New scene
 newScoreText.text = PlayerPrefs.GetString("score");

Method 2:

 // Static variables are not stored in script instance, they are stored in stack, that means they are "remembered" even on scene change
 // Scene is just used to get name of current scene
 
 using UnityEngine.SceneManagement;
 
 private string sceneName;
 
 public static string score = player.position.x.ToString("0");
 
 private void Start() {
     sceneName = SceneManager.GetActiveScene().name;
 
     if (sceneName == "GameOverScene") {
        newScoreText.text = score;
     }
 }
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 DaProtato · Jan 02, 2018 at 08:59 PM 0
Share

Thanks for the reply!

The issue with $$anonymous$$ethod 1 is that when I store the value as a string, the text in the game doesn't update with the player movement which makes the newScoreText value incorrect.

The issue with $$anonymous$$ethod 2 is that when I type line, public static string score = player.position.x.ToString("0"); I get the error "a field initializer cannot reference the nonstatic field..." underlining "player".

If you could figure out how to fix either method, it would be greatly appreciated :D

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

124 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple EvenetSystems in Scene - only have 1 after searching though 1 Answer

When die - reset score from the last scene 2 Answers

How to get rid of the duplicates created by DontDestroyOnLoad? 3 Answers

One PlayerPref not saving 1 Answer

Worldspace UI in wrong position/scale after scene change 0 Answers


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