Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iRaMb0 · May 29, 2013 at 05:29 AM · variableslevelsxp

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kiwi koder · May 31, 2013 at 12:53 AM 0
Share

accept an answer as both answers solve your problem

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iRaMb0 · May 29, 2013 at 04:33 PM 0
Share

How would i be able to store the variables?I'm very confused :<

avatar image TonyLi · May 30, 2013 at 02:58 PM 1
Share

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;
     }
 }
avatar image TonyLi · May 30, 2013 at 03:00 PM 1
Share

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.

avatar image iRaMb0 · May 31, 2013 at 05:55 PM 0
Share

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

avatar image TonyLi · May 31, 2013 at 09:04 PM 1
Share

If I understand you correctly:

  1. The first script is attached to the cube game object. What's the name of this script?

  2. The second script (CubicLevel?) is attached to the player character game object.

  3. 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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges