- Home /
Preventing variable from resetting when level loads
I've created a game with 5 levels, and each level contains objects that the player picks up and delivers to a certain location. I already have a score system that uses DontDestroyOnLoad (transform.gameObject); to keep the score in the next scene.
My problem is this: I've created a 'quota'system that keeps track of how many objects the player has left to gather to complete the level. This works fine currently, at least for gathering the items and progressing to the next level. When the player dies, however, the level resets (using Application.LoadLevel (Application.loadedLevelName);), but the 'quota'for the level is reset. Initially I tried making it a regular var : int, so it could be edited in the inspector, and passing this to a static var that could be accessed by the player's object. When I discovered the restarting issue, I tried simply hard-declaring it to no avail.
Can anybody see what I'm doing wrong here?
Here's the code attached to a GuiText (that displays the quota): static var bottleQuota : int;
function Awake () {
DontDestroyOnLoad (transform.gameObject);
bottleQuota = 5;
}
function OnGUI() {
guiText.text = "Bottles to collect : " + bottleQuota;
}
...and here is the relevant code attached to the player character:
bottleQuotaCounter.bottleQuota -= cargo;
Cheers,
Answer by DavidDebnar · Jul 10, 2011 at 01:50 PM
David
If I use PlayerPrefs, I'll still need to 'set' the 'quota' per level, and when the level reloads it will run that 'setting' code again, which will make this solution useless, yes?
Yeah that occurred to me this morning. I'll test it this evening.
Your answer
![](https://koobas.hobune.stream/wayback/20220612140315im_/https://answers.unity.com/themes/thub/images/avi.jpg)