- Home /
How can i make it give u a certain xp everytime u kill someone?
basically i have this script
`var guiTextEXP : GUIText; var accumulatedExperiencePoints : int = 0; var levelExpRequirements : int = 1000; var currentLevel : int = 1;
function Update () {
if(accumulatedExperiencePoints >= levelExpRequirements) { levelExpRequirements += 1000; currentLevel += 1; }
guiTextEXP.text = accumulatedExperiencePoints+"/" + levelExpRequirements + "|" + currentLevel; }`
and it works fine but now i need something to make it give me a certain amout of xp per kill...
Answer by · Aug 20, 2010 at 06:43 AM
Updated
Broadly speaking, you'd probably want each enemy to have a script containing variables dictating their health/xp. Then every time you applied damage to them, you'd check if their health was <= 0, and if so, kill them (death sound, etc) and increment the player xp by the specified amount.
Looking at your code (link in comments), you could add a variable to the EnemyDamageReceiver (assuming it's attached to every enemy).
var expVal : int = 20;
Then in ApplyDamage(), you'd add a line to find the Player, get the ExperienceSystem component (assuming it's attached to the Player) and then GiveXP to a certain value.
if (hitPoints <= 0.0) {
GameObject.Find("Player").GetComponent(ExperienceSystem).GiveXP(expVal);
// Start emitting particles
(everything else here...)
}
In the ExperienceSystem, you'd need to add a GiveXP function. Mike provided a decent example in the link I've commented, so I'll reuse it here.
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;
}
}
Note that I also extracted the level-up check from your Update() loop, because it's not necessary to check that every frame - only when the player actually gains experience.
Hope this sets you on the right track.
You seem to keep asking the same question, adding slightly more detail each time (Q18343: Experience System & Q18579: Experience System). Your script here is just a copy/paste of xToxicInferno's answer from the second link. Do you have any other code of your own?
Happy to take a look at bugs in any code you might have, but take a crack at it first. Or, if you're inexperienced with javascript/games-programming, you might want to try one of the tutorials to get a hang of Unity.
You've left out a lot of detail that would actually make it possible to write code that directly helps you. Do you have Enemy GameObjects? Do they have scripts on them? Requesting your code is so we can help, not s$$anonymous$$l it!
$$anonymous$$ike has written a good answer here: http://answers.unity3d.com/questions/10505/variables-acesssed-by-other-scripts-and-change-int-values that gives an example of how to do what I described. You should definitely also read through the Reference Documentation (http://unity3d.com/support/documentation/ScriptReference/index.html) and work through a tutorial or two.
Okay, so in the EnemyDamageReceiver.js you could add a variable that stores the xp. Then in ApplyDamage() where you 'Start emitting particles' (enemy is dead), you increment the Player's experience by the amount that the enemy gives. I would also change the level-up system so it only checked when you actually received XP. I'll update my answer accordingly.
Doh, I didn't copy/paste correctly from your script! Updated my answer - missed a line from GiveXP.
In my experience, those errors are usually indicative of an error further up the page. $$anonymous$$ake sure you've got equal numbers of { and } ? The script looks fine to me...
ok i checked it out and was able to get rid of the error but there 2 more problems... gui text still dosnt change (
Your answer
Follow this Question
Related Questions
Xp Calculation Bar 1 Answer
Dynamic Level Progress Bar 1 Answer
Experience system for multiple skills 1 Answer
Experience System 1 Answer
xp math equation help 1 Answer