- Home /
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! :)
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.
I suggest only tracking requiredExp and expToNextLevel. Having three variables isn't giving you any extra functionality.
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.
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.
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?
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.
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.
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.
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.
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 :/
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
Would you be able to help regarding this particular setup?
Your answer
![](https://koobas.hobune.stream/wayback/20220613121820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Resticing variables to whole numbers 3 Answers
Scripting Tut. 1 Answer