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++;
}
}
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.
@Dread$$anonymous$$yller I added some code that you requested.
Your answer
Follow this Question
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