- Home /
Variables carrying on to other levels?
How can i make a script so that variables carry on from scene to scene? I want the xp of the player to carry on to other scenes?! I tried using LoadLevelAdditive but it messed up the levels quite a lot...Help?
Answer by TonyLi · May 29, 2013 at 03:56 PM
PlayerPrefs is good for saving and loading data between play sessions (i.e., saved games).
When changing levels during the same play session, instead consider keeping the variables in a script on game object that will not be destroyed when loading a new level. To keep a game object from being destroyed, use DontDestroyOnLoad().
function Start() {
DontDestroyOnLoad(this.gameObject);
}
Alternatively, you can store the variables in a singleton or static class that isn't a MonoBehaviour. Since it won't be attached to a game object, it will never get destroyed.
How would i be able to store the variables?I'm very confused :<
First, add the Start() function from my answer. This will guarantee that the object that your script is attached to will carry on from scene to scene.
Second, change:
if (curXp == mapXp)
to:
if (curXp >= mapXp)
Otherwise your player will never level up if he gets more than maxXp.
Then, let's say you have a Wizard that grants you 100 XP. Add something like this to his script:
function GrantXP() {
// Replace PlayerScript with the name of your script:
var player : PlayerScript = GetComponent(PlayerScript);
if (player != null) {
player.curXp += 100;
}
}
Note that this allows your player (and its attached scripts) to carry on from scene to scene within the current play session. If you also want to save a game between play sessions, use PlayerPrefs for that, like kiwi koder suggested.
god damn it!!! I'd try using kiwi's answer but he deleted it(?) and my head is gonna explode!!!!!!!
Let's take it from the start please:
||The ONLY way i can get xp is through little cubes and this is the script attached to them:
#pragma strict
var Target : GameObject;
var Target2 : $$anonymous$$eshRenderer;
var Particles : GameObject;
var CurXp : CubicLevel = GetComponent(CubicLevel);
function Start () {
Particles.active=false;
}
function Update () {
}
function OnTriggerEnter(){
//~~~~~~~~
if (CurXp != null) {
CurXp.curXp += 10;
}
//~~~~~~~~~
Particles.active=true;
Destroy(Target2);
yield WaitForSeconds(2);
Destroy(Target);
}
have to manually assign the var Curxp from CubicLevel(my level script) and a gameobject with CubicLevel attached to it is holding those var and the problem is that since it continues from scene to scene i cant assign them so i dont get any xp-
||This is the script that holds leveling variables and such :
var curXp : int = 0;
var gkui : boolean = false;
var maxXp : int = 500;
var UpgradeP : int = 0;
var Trail1 : GameObject;
var Trail2: GameObject;
var Fireworks : GameObject;
var $$anonymous$$ySkin : GUISkin;
var WalkSpeed :ThirdPersonController = GetComponent(ThirdPersonController);
var JumpHeight : ThirdPersonController = GetComponent(ThirdPersonController);
var level : int = 1;
function Start(){
DontDestroyOnLoad(this.gameObject);
Trail1.active=false;
Trail2.active=false;
Fireworks.active=false;
}
function Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Tab)){
gkui = true;
}
else{
gkui=false;
}
if(curXp >= maxXp) {
levelUpSystem();
PlayerPrefs.SetInt("Player level", level);
PlayerPrefs.Save();
}
}
function levelUpSystem () {
UpgradeP++;
curXp = 0;
maxXp = maxXp + 50;
level++;
WalkSpeed.walkSpeed +=10;
JumpHeight.jumpHeight+=0.5;
Trail1.active=true;
Trail2.active=true;
Fireworks.active=true;
yield WaitForSeconds(10);
Trail1.active=false;
Trail2.active=false;
Fireworks.active=false;
}
function OnGUI(){
GUI.skin = $$anonymous$$ySkin;
if(gkui){
GUI.Box(Rect(200,825,115,25),"");
GUI.Label(Rect(205,825,120,25),"Level " + level + " XP " + curXp + " / " + maxXp);
GUI.Box(Rect(510,825,115,25),"");
GUI.Label(Rect(515,825,125,25),"Upgrade Points " +UpgradeP);
}
}
The variables DO carry on from scene to scene but my problem is the one that i mentioned earlier
If I understand you correctly:
The first script is attached to the cube game object. What's the name of this script?
The second script (CubicLevel?) is attached to the player character game object.
The cube game object has a trigger collider.
If this is the case, then you want to get the CubicLevel component that's on the player character, which is the object that's colliding into the cube's trigger. To the cube, this object is known as "other" in the OnTriggerEnter() function.
To do that, change the first script's OnTriggerEnter() function to:
function OnTriggerEnter(other : Collider) {
var c : CubicLevel = other.GetComponent(CubicLevel);
if (c != null) {
c.curXp += 10;
}
}
If my understanding was incorrect, maybe you need to reacquire the reference to CubicLevel after loading a new level. In this case, you could write an OnLevelWasLoaded() function: http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.OnLevelWasLoaded.html
Your answer
Follow this Question
Related Questions
Problem Setting Data Onto Another Script 1 Answer
New scene for minigame or not? 1 Answer
Display Audio Analysis and levels with objects 0 Answers
How come when I restart my level it doesn't work? 2 Answers
CHaracter walking through walls 3 Answers