- Home /
Scoring and Variables
Hey guys this is a 2 part so please bear with me. I'm currently creating a game that has multiple levels and as the levels go up, I want the difficulty to increase. I also wanted to maintain the score as the scene changes. So my questions are as follows
How can I increase the variable of a falling object to make the level more difficult as it goes up?
How do I keep the user's score between levels so that in the next scene, the score doesn't reset?
For example, what I currently have in terms of falling objects is the following:
function Start () {
//When your object first gets created
for (var i:int=1; i <= 1; i++) {
InvokeRepeating("SpawnZ", 0, 0.7);
}
}
I want the variable that increases the rate of them falling to increase so they will fall faster and make the Z's harder to catch. I anticipate and appreciate any help that you guys can give me!
Thanks in advance,
Slyy
Answer by Itinerant · Oct 24, 2012 at 11:43 PM
For keeping a score, use DontDestroyOnLoad(). This will let you keep an object between scenes, and all variables on the scripts it holds will be kept as well.
I'm not sure on the other one: do you want to change it based on which scene you're in, or based on how long you've been in there, or your score, or something else?
If it's by scene, I'd use Application.loadedLevel to check which scene number, then base it on that. Switch that 0 with a variable, then change the variable based on the scene number or whatever.
my score is actually an int variable...is there a way to save that? Because the DontDestroyOnLoad() doesn't apply to integers
aside from saving it to a file, then reloading it in the new level, no. And I highly recommend against that, Itinerant's solution is the best way to go. Put a DontDestroyOnLoad component on whatever object is holding your int, and you'll be good to go.
well the thing is, I don't have my playerScore variable tied to any objects....it's applied to a GUI Style via the playerScore.ToString() method so I'm still lost as to how to use the DontDestroyOnLoad() in order to save my score because it doesn't apply to an integer value. Would I apply it to my GUI Style?
Your GUI Style is attached to a GameObject, you will want to apply DDOL to it by calling: DontDestroyOnLoad(gameObject); within your script
Ok I understand now and I got that to work...now how would I go about changing the GUI $$anonymous$$aterial between levels if I'm not destroying the object it's attached to? For example: my GUI Style $$anonymous$$aterial is a light blue on level 1 but I want it to be a red material on Level 2.
Your answer
![](https://koobas.hobune.stream/wayback/20220613082829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer
Getting MouseEnter to Another Object 1 Answer
Transform var between scripts 2 Answers
Scoring System 1 Answer
Mob Spawner, choosing a random mob 2 Answers