I am wondering how to save a int in the background
Hi, I have been trying to make an incremental game. I just started making it, but cant figure out how to save my score in the background. I have tried useing DontDestroyOnLoad(gameObject); , but this only returns with "DontDestroyOnLoad only work for root GameObjects or components on root GameObjects." I can not figure out how to fix this. I have some ide what roots are, but i dont know how it works. So my first question is what is the root?
my second question is how do i save my score.(My game works with a button getting pressed, and a number going up. This nuber can go up for example 2 /sec) So i am wondering, how do i save this number in between scenes?
Answer by Commoble · Feb 06, 2018 at 08:50 PM
Firstly, A root gameobject is a gameobject with no parent.
Secondly, for data to persist between scenes, you'll need to save it in a file somewhere. This official unity video has a section on saving and loading data between scenes.
That was what i thaught that a root gameObject was, but i can not figure out what to do when the canvas is the root. And I did watch most of that video, but he goes over when the text follows to the next scene. I want the number to stay on the first scene, but in the background so i can use it on other scenes. Do you know a way I can do this? And do you have a script as an example?
Answer by x4637x · Feb 07, 2018 at 03:54 AM
You could try using PlayerPrefs. Check PlayerPrefs.SetInt for example.
Your answer
Follow this Question
Related Questions
how to disable dontdestroyonload on a specific scene 2 Answers
unable to use dontdestroy on load 1 Answer
Unity/C# Respawn and score Problem 1 Answer
how to show score using panel 0 Answers
I need help with my script please 1 Answer