- Home /
Variable Doesn't Update in "Update" function
Ive been working on fixing this code for a day and a half now and can not figure it out. I've googled and googled but cant find a decent answer.
Im trying to work with this code... (Javascript)
#pragma strict
static var playerDamage : int;
var Distance : float;
var MaxDistance : float = 1.5;
static public var TheDamage : int = 2;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
if ((MeleeLevel.MeleeLevel == 1) && (MeleeLevel.MeleeLevel < 5))
{
playerDamage = 2;
}
if ((MeleeLevel.MeleeLevel >= 5) && (MeleeLevel.MeleeLevel < 10))
{
playerDamage = 3;
}
if ((MeleeLevel.MeleeLevel >= 10) && (MeleeLevel.MeleeLevel < 20))
{
playerDamage = 4;
}
if ((MeleeLevel.MeleeLevel >= 20) && (MeleeLevel.MeleeLevel < 30))
{
playerDamage = 5;
}
if ((MeleeLevel.MeleeLevel >= 30) && (MeleeLevel.MeleeLevel < 40))
{
playerDamage = 7;
}
if ((MeleeLevel.MeleeLevel >= 40) && (MeleeLevel.MeleeLevel < 50))
{
playerDamage = 9;
}
if ((MeleeLevel.MeleeLevel >= 50) && (MeleeLevel.MeleeLevel < 60))
{
playerDamage = 12;
}
if ((MeleeLevel.MeleeLevel >= 60) && (MeleeLevel.MeleeLevel < 70))
{
playerDamage = 15;
}
if ((MeleeLevel.MeleeLevel >= 70) && (MeleeLevel.MeleeLevel < 80))
{
playerDamage = 20;
}
if ((MeleeLevel.MeleeLevel >= 80) && (MeleeLevel.MeleeLevel < 90))
{
playerDamage = 25;
}
if ((MeleeLevel.MeleeLevel >= 90) && (MeleeLevel.MeleeLevel < 100))
{
playerDamage = 31;
}
if (MeleeLevel.MeleeLevel == 100)
{
playerDamage = 37;
}
TheDamage = playerDamage * Random.Range(0,10);
}
if(Input.GetButtonDown("attack02"))
{
if ((MeleeLevel.MeleeLevel == 1) && (MeleeLevel.MeleeLevel < 5))
{
playerDamage = 2;
}
if ((MeleeLevel.MeleeLevel >= 5) && (MeleeLevel.MeleeLevel < 10))
{
playerDamage = 3;
}
if ((MeleeLevel.MeleeLevel >= 10) && (MeleeLevel.MeleeLevel < 20))
{
playerDamage = 4;
}
if ((MeleeLevel.MeleeLevel >= 20) && (MeleeLevel.MeleeLevel < 30))
{
playerDamage = 5;
}
if ((MeleeLevel.MeleeLevel >= 30) && (MeleeLevel.MeleeLevel < 40))
{
playerDamage = 7;
}
if ((MeleeLevel.MeleeLevel >= 40) && (MeleeLevel.MeleeLevel < 50))
{
playerDamage = 9;
}
if ((MeleeLevel.MeleeLevel >= 50) && (MeleeLevel.MeleeLevel < 60))
{
playerDamage = 12;
}
if ((MeleeLevel.MeleeLevel >= 60) && (MeleeLevel.MeleeLevel < 70))
{
playerDamage = 15;
}
if ((MeleeLevel.MeleeLevel >= 70) && (MeleeLevel.MeleeLevel < 80))
{
playerDamage = 20;
}
if ((MeleeLevel.MeleeLevel >= 80) && (MeleeLevel.MeleeLevel < 90))
{
playerDamage = 25;
}
if ((MeleeLevel.MeleeLevel >= 90) && (MeleeLevel.MeleeLevel < 100))
{
playerDamage = 31;
}
if (MeleeLevel.MeleeLevel == 100)
{
playerDamage = 37;
}
TheDamage = playerDamage * Random.Range(0,10) * 1.45;
}
}
function PlayerDamage ()
{
var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApplyDammage", TheDamage, SendMessageOptions.DontRequireReceiver);
}
}
Debug.Log("Player Damage " + playerDamage + " Damage with random " + TheDamage);
}
The code takes my "Melee Level" and adds a damage value to playerDamage depending on my Melee Level. Then takes playerDamage * a random number to = TheDamage(Var)
but the PlayerDamage() function will not read the new TheDamage in the Update() function, it will always read it as "2" where I created the variable at the top.
so everytime I hit the enemy the damage dealt is always 2.
I hope my explanation is understandable.. any help is appreciated. thanks!
Are you sure "Fire1" and "attack02" buttons are correctly configured? I mean, do those "if" evaluate to true?
Other than that, even if your $$anonymous$$eeleLevel is not in the [1,100] range the variable TheDamage should change.
Add a "Debug.Log(TheDamage);" line after the lines where you change the TheDamage value to make sure the code at that point was executed.
Have you tried debugging? Put Debug.Log calls inside the if-blocks so you see if you ever go into those ifs. You can also print out the $$anonymous$$eleeLevel and playerDamage to see where the mistake happens.