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 /
  • Help Room /
avatar image
0
Question by Silentkillar · Dec 18, 2017 at 10:11 AM · objectsienumeratordata storage

Scene changes, data save and ienumerators? HELP

Hey,

My last question on here got deleted which is really annoying, because other answers on similar questions doesn't help me. Don't remove my question!

So I'm making this clicker game, and I have two scenes so far. The "MainScene" and the "StatScene". You get coins by clicking and buying upgrades on the "MainScene", this is where I have my main script on a gameobject, which controls everything. I then wanted to implement a feature where they could click on a "Stats" button, to see their stats. When I click on this stats button and go to my "StatScene", all my stats are showing, exactly how I want it to. Now, my problem is, when I click my "back" button, all progress is reset, my IENumerator TimerRoutine which gives coins every second is stopped, my texts and buttons are not referenced in the script either anymore. (Like in the inspector, they're not assigned/attached to the script.)

How can I make so the data saves/the coins still adds up in my other scene, WHILE I can access the data in my "StatScene", without losing progress?

Thanks in advanced. Here's some of my code.

 void Awake () {
         DontDestroyOnLoad(transform.gameObject);
         StartCoroutine(TimerRoutine());
         if (FindObjectsOfType(GetType()).Length > 1)
         {
             Destroy(gameObject);
         }

Here's my scene change code, here I tried to store my data in PlayerPrefs. It worked. But the script is still "paused" when I return to my MainScene. My main script on my main object in mainscene still has the values stored in it, but the texts are not linked which results in the data not showing on the screen. Also, my TimerCoroutine which is basically a timer, that adds coins every second, is stopped.

 void SwitchSceneStats()
     {
         PlayerPrefs.SetFloat("Ghz", ghz);
         PlayerPrefs.SetFloat("xtra", xtraBtcPerClick);
         PlayerPrefs.SetInt("timesClicked", timesClicked);
         PlayerPrefs.SetInt("itemsOwned", itemsOwned);
         PlayerPrefs.SetFloat("alltimeBTC", alltimeBtc);
         PlayerPrefs.SetFloat("btcSpent", btcSpent);
         PlayerPrefs.SetInt("timePlayed", timePlayed);
         PlayerPrefs.SetInt("hasTor", hasTor);
         SceneManager.LoadScene("StatsScene");
         StopAllCoroutines();        
     }

Here's my IENumerator TimerRoutine:

 public IEnumerator TimerRoutine()
     {
         while (currentBtc < 99999999999)
         {
             yield return new WaitForSeconds(wait);
             currentBtc += ghz;
             alltimeBtc += ghz;
             btcCounter.text = currentBtc.ToString("#.00000000");
             textMMPrice.text = miniMinerPriceToUSD.ToString("Price: $##0.00");
             btcKurs = currentBtc * 18000;
             btcToDollars.text = btcKurs.ToString("$#0.000");
             textAsicPrice.text = asicMinerPriceToUSD.ToString("Price: $##.00");
             textTorPrice.text = torBrowserPriceToUSD.ToString("Price: $##.00");
             timePlayed++;
         }
     }

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 DreadKyller · Dec 19, 2017 at 12:37 AM 0
Share

It would be useful to see some example code, namely the code related to the scene switching and in that general area. $$anonymous$$ake sure you've marked your main object to not get removed by calling DontDestroyOnLoad(gameObject) if you haven't, and make sure you don't have a duplicate of that object in your other scene, as the actual object is carried over. When scenes switch the objects in them are deleted and then re-added, so if you have object holding data, that data will get reset, if your main script stores the data then using the DontDestroyOnLoad will allow it to maintain that data across scenes. Note that because objects are removed, if you have any Objects themselves linked to the scripts in the inspector, these links may break, so using GameObject.Find("name") to re-establish those links may be a wise choice if this is your problem.

avatar image Silentkillar · Dec 19, 2017 at 07:57 AM 0
Share

@Dread$$anonymous$$yller I added some code that you requested.

0 Replies

· Add your reply
  • Sort: 

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

120 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

Related Questions

How to hold and move a object using other object? 3 Answers

basic Questions about MeshFilter:Optimize mesh 1 Answer

How to add a script to a parent object and not every individual child object. 1 Answer

Does the sequence in which I keep the objects in a scene matter really? 1 Answer

How to check if any GameObject equals any other GameObject 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