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! :slight_smile:

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.

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.

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: