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
0
Question by ihsanyunus · Mar 05, 2021 at 06:36 AM · programmingprogramming-basics

How to keep Text file going in different scenes?

I have made a stopwatch text file to calculate how fast the player can finish the game, but in order to do that I know that I need to use the DontDestroyOnLoad. But where do I put it? I have the script file so can someone tell me where to put it? Thank you very much..

[1]: /storage/temp/177042-capture.jpg

capture.jpg (47.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Mar 05, 2021 at 07:08 AM

You can put it anywhere in your scene and the moment that it is called a persistent scene will be created and the game object will then be in that persistent scene which is like a scene layered on top of the scene you have running. When you load another scene, that persistent scene will continue to be there with the object which has the stop watch functionality. If classes in the new scene have to access that object/stopwatch class, you do as you would anytime you want to access any other class:


 public class Stopwatch : MonoBehaviour 
 {
     public Text stopwatchText;
 
     private void Start()
     {
         DoNotDestroyOnLoad(gameObject);
     }
 }



Now access in in the next scene:


 public class SomeClass : MonoBehaviour 
 {
     private Text stopwatchText;
 
     private void Start()
     {
         //Really slow, but no tag needed
         stopwatchText = FindObjectOfType<Stopwatch>().stopwatchText;
 
         //Alternatively you can access it by the tag which is a lot faster. The game object from the stopwatch would need this tag of course
         stopwatchText = GameObject.FindWithTag(“StopwatchTagName”).GetComponent<Stopwatch>().stopwatchText;
     }
 }


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 ihsanyunus · Mar 05, 2021 at 08:39 AM 0
Share

Aww, Im afraid DontDestroyOnLoad only works on gameObjects and root components on gameObjects, I really appreciate your answer. Thank you!

avatar image highpockets ihsanyunus · Mar 05, 2021 at 08:50 AM 0
Share

Yes, that is correct. If you need to persist the stopwatch with a child of a game object, then you either need to use DontDestroyOnLoad(transform.root.gameObject); to include the parent or remove the child and place it in at the root. Or you need to just persist the data instead with PlayerPrefs (un-secure) or in the file system (secure) to a new stopwatch in the next scene

avatar image
0

Answer by b4guw1x · Mar 05, 2021 at 10:52 AM

If the text isn't that long, you can simply keep it with player prefs.

 PlayerPrefs.SetString("PlayerTime","xxxxx");

When you want to acces it you can use

 PlayerPrefs.GetString("PlayerTime");




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

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

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

Related Questions

Multiple Cars not working 1 Answer

Difference between while loop and dowhileloop? 1 Answer

Need an in-depth explanation on this code. 2 Answers

Space Flight Tutorial 1 Answer

How to make if limit is reached then make sliders only slide back in ui (not forward) 2 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