Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 cwaite84 · Nov 13, 2013 at 07:40 PM · javascriptleveling

Need some help concerning script variables.

Hello. Basically I have an experience system I am trying to implement. I want the player to be able to gain experience points PER KILL. I have two scripts one that is called EnemyDamageReceiver.js, and the other called ExperienceSystem.js. So I want it so when this is true : `

 if(hitPoints <= 0.0){
          GameObject.Find("Player").GetComponent(ExperienceSystem).GiveXP.(expVal);
 }

Keep in mind that the script containing this code is attached to my enemy. Also in the same script I do have a variable set for expVal that looks like this:

 var expVal : int = 20;

So now in the ExperienceSystem.js, it looks like this.

 var guiTextEXP : GUIText;
 var accumulatedExperiencePoints = 0;
 var levelExpRequirements = 1000;
 var currentLevel = 1;
 var xp : int;
  
 function GiveXP(addedXP : int)
 {
     if (addedXP < 0)
     {
         Debug.Log("Can't remove exp!");
         return;
     }
  
     xp += addedXP;
  
     if (accumulatedExperiencePoints >= levelExpRequirements)
     {
         levelExpRequirements += 1000;
         currentLevel += 1;
         guiTextEXP.text = accumulatedExperiencePoints+"/" + levelExpRequirements + "|" + currentLevel;
     }
 }

Now I simply want the value that is assigned to the expVal variable that is on my EnemyDamageReceiver.js to be sent to my accumulatedExperiencePoints variable on my ExperienceSystem.js whenever the enemy dies so that it gives him experience points. I have tried MANY different way to attempt this but have failed. I am just simply unclear and unsure what I am doing wrong. I am not that experienced in UnityScript. So please bare with me. That is why I am asking for help here so that I can get the hang of these simple things to move on to bigger things. Thank you so much if you are willing to help me. All ideas are appreciated! thank you again! :)

Comment
Add comment
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

3 Replies

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

Answer by AndyMartin458 · Nov 13, 2013 at 09:30 PM

AccumulatedExperiencePoints is not being added to from what I see. You add to the xp, but then you never use it. That is the problem keeping you from leveling up.

I have a few suggestion that might help you make the class work as you want.

  1. I suggest only tracking requiredExp and expToNextLevel. Having three variables isn't giving you any extra functionality.

  2. For good measure, add a Debug.Log statement to contrast the Can't remove exp statement. Show in your log that you are adding exp and how much is being added.

  3. This one is a preference, but it is the general approach for experience, gold pieces, etc. The hit points for the bad guy should be an int instead of a double, so the comparison should only compare to 0 instead of 0.0

UPDATE - The update function is not going to be called after you destroy the game object. Make sure to send the experience before destroying it.

Comment
Add comment · Show 17 · 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 cwaite84 · Nov 13, 2013 at 11:24 PM 0
Share

Added this to my EnemyDamageReceiver.js.

 var expScript : TestEXP;
 
 function Update(){
     if(hitPoints <= 0.0){
         expScript.addEXP = true;
     }
 }

$$anonymous$$ade a new script called TestEXP.js and this s what it contains

 var expGained : int = 0;
 var currentLevel : int = 1;
 var maxLevel : int = 100;
 var expNeeded : int = 200;
 var expIncrement = 1.5;
 var enemyScript : EnemyDamageReceiver;
 var levelUP : boolean = false;
 var addEXP : boolean = false;
 
 function Update(){
     if(addEXP){
         addExp();
     }
     
     Debug.Log(expGained + ' Exp Gained Var');
 }
 
 function addExp(){
     expGained += enemyScript.expVal;
     
     addEXP = false;
     
     
     if(expGained >= expNeeded){
         levelUP = true;
             if(levelUP){
                 levelUp();
             }
     }
     
 }
 
 function levelUp(){
     expNeeded = expNeeded * expIncrement;
     currentLevel++;
     levelUP = false;
 }

Does that look like it would work?

avatar image AndyMartin458 · Nov 13, 2013 at 11:27 PM 0
Share

You don't really need that levelUp variable. It does look like it would work. It's a lot different than what you showed earlier. The one problem could be if you killed multiple enemies somehow before the next update call, but that probably won't happen.

avatar image cwaite84 · Nov 13, 2013 at 11:32 PM 0
Share

Yeah really seems to not wanna work still. I was using the idea that the person above had implemented. That is why it is different. I just wanted to see if it would work that way.

avatar image AndyMartin458 · Nov 13, 2013 at 11:33 PM 0
Share

what is it that doesn't work?

avatar image cwaite84 · Nov 14, 2013 at 01:41 AM 1
Share

Well Ill be damned.... That was the issue. Well Dont I feel like a smart one. Hey thank you so much for the help... I really appreciate it. Also thank you for being patient with me.

Show more comments
avatar image
0

Answer by an-ch · Nov 13, 2013 at 09:25 PM

Well im not very experienced but i think i have a posible solution. Chance your EnemyDamageReceiver.js to :

     if(hitPoints <= 0.0){
     ExperienceSystem.getexp=true;
     }

In your ExperienceSystem.js add this :

  public static var getexp=false;
     function Update () {
         if(getexp)
            {
              GiveXP(theaddedexperience);
            }

and to the GiveXP function add : getexp=false; right before the function closes.

So each time an enemy dies it changes the getexp from false to true which triggers the GiveXP function which makes the getexp false again.

Hope i was helpfull.

Comment
Add comment · Show 1 · 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 cwaite84 · Nov 13, 2013 at 11:16 PM 0
Share

Seems like the right track but the wrong train. I did this, I made a test script to go in here. I added this to my EnemyDamageReceiver.js

 var expScript : TestEXP;
 
 function Update(){
     if(hitPoints <= 0.0){
         expScript.addEXP = true;
     }
 }

Then I made a script called TestEXP.js and it looks like this.

 var expGained : int = 0;
 var currentLevel : int = 1;
 var maxLevel : int = 100;
 var expNeeded : int = 200;
 var expIncrement = 1.5;
 var enemyScript : EnemyDamageReceiver;
 var levelUP : boolean = false;
 var addEXP : boolean = false;
 
 function Update(){
     if(addEXP){
         addExp();
     }
     
     Debug.Log(expGained + ' Exp Gained Var');
 }
 
 function addExp(){
     expGained += enemyScript.expVal;
     
     addEXP = false;
     
     
     if(expGained >= expNeeded){
         levelUP = true;
             if(levelUP){
                 levelUp();
             }
     }
     
 }
 
 function levelUp(){
     expNeeded = expNeeded * expIncrement;
     currentLevel++;
     levelUP = false;
 }

The exp Gained still refuse to update. Not sure why though :/

avatar image
0

Answer by RyanZimmerman87 · Nov 13, 2013 at 11:24 PM

I do a pretty nice in depth answer related to XP and leveling up including PlayerPrefs in this other thread if you wanna chcek it out:

http://answers.unity3d.com/questions/567703/how-to-maintain-a-learning-by-using-skill-system.html

Comment
Add comment · Show 2 · 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 cwaite84 · Nov 13, 2013 at 11:25 PM 0
Share

$$anonymous$$ost certainly! :)

avatar image cwaite84 · Nov 14, 2013 at 12:01 AM 0
Share

Would you be able to help regarding this particular setup?

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

19 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Flipping textures 0 Answers

How do I invert the Y axis in Penelope tutorial? 3 Answers

Rotating a Sphere with the mouse? 1 Answer


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