Help With Enemy on death Exp.

I have my enemy set up in such a way that no matter which state/animation its in if his health reaches 0 he dies and plays appropriate animation. However when I added the Exp in to this I noticed it was calculating the Exp gained every frame. Which means the player goes from level 1 to about 6 to 8 depending on frame rate at that time. I was wondering how to only call the exp function only once.

Now I started it out as a simple void function.

void AwardPlayerExp()
{
      SaveLoad.saveLoad.curExp += awardedExp;
}

I’ve tried to use true/ false statements but didn’t seem to work either.

Then I tried to do a IEnumerator with a break when return null didn’t work.

IEnumerator AwardPlayerExp()
    {
        if (SaveLoad.saveLoad.curExp <= SaveLoad.saveLoad.maxExp)
        {
            SaveLoad.saveLoad.curExp += awardedExp;
            yield break;
        }
    }

The effect was still the same. Only thing is i think this method was alittle easeir on the FPS because i only made it to level 6 instead of 8. The awardedExp is equal to 50.

I could really use some help here!! oh better show you how the enemy is receiving this IEnumerator;

void Idle ()
    {
        if (distents > lookAtDistents && distents > lookAtDistents && health > 0) {
            animationControl.Play ("Idle");
        }else if(health <= 0) {
            enemyHealthSlider.value = health;

            StartCoroutine(AwardPlayerExp());
            Destroy (this.gameObject, 2f);
            animationControl.Play ("Death");
           
        }
    }

Now I know im calling this in the Idle function in the update function. so this calculating the exp as i mentioned up above. I just needing to call this ounces.

An again any and all help would be great!!

it is quite simple , for the duration that health <= 0 you keep calling AwardPlayerExp , so your value keeps changing , okay thats not what you want as you said.

to remedy the situation , you can add a global bool variable to switch on and off depending on the death state , you start off with the value being true then switch it to false the first frame the health reaches your target or start off false and switch to true during death , add a check then with this bool to clamp your code flow only once.

by the way why are you checking distents twice .

or you can move some of the code towards an OnDestroy() event .

I cant remember why i was checking it twice … was a bit ago. ill check that out. as for the global bool ill check that out and let you know. Thanks for responding! Helps a lot!

well… tried to do the what you said but found out I had other bits of code messing with what i was trying to do. Plus the way I was going about things was using to much resources. This was slowing the FPS way down; so I got to start over and reevaluate the process. Pretty much I was using the update function way to much to run all my functions. Not giving up just going to take my time an approach it differently.

Keep a variable to know the state of your enemy.
bool isAlive = true;

in your update function

if(isAlive)
{
    if(health <= 0)
    {
        isAlive = false;
        //Award xp
        //PlayDeathAnim;
    }
}
else
{
    if(deathAnimIsOver)
    {
        Destroy(gameObject);
    }
}