- Home /
The question is answered, right answer was accepted
Problem with variables,Problem with identifying a variable
I have one variable that I send to another script. And here I have a problem: this variable is displayed as two different . So I don't know why it happens and what can I do ... public class HealthScript : MonoBehaviour {
public int curHealth;
public bool CanGetHit;
void Start()
{
CanGetHit = true;
curHealth = 100;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Spear" && CanGetHit == true)
{
col.gameObject.GetComponent<DamageSc>().Damage = damage;
Hited(damage);
}
}
void Hited(int damage)
{
if (CanGetHit == true)
{
curHealth = curHealth - damage;
CanGetHit = false;
}
}
}
, ![alt text][2]
Answer by Honorsoft · Jan 24, 2018 at 11:57 PM
I'm a little confused because you say "this variable is displayed as two different", so I'm not sure exactly what that means or which "this" variable refers to, but.. One thing I noticed is in your line col.gameObject.GetComponent<DamageSc>().Damage = damage;
it looks like you are trying to get the amount of damage that the weapon that collided with the character does, in that case, I think you meant: damage = col.gameObject.GetComponent<DamageSc>().Damage;
and you would have to declare a public int damage;
at the top of this object's script. The way you had it, the col.gameObject's Damage
was being set to this object's damage
(which in this script stays at zero) Also, you will need to reset the 'CanGetHit' to true after a bit of time passes.
-It might help for us to see your weapon object's Damage script too.
Answer by unity_S0ZCLiVP1XXH-w · Jan 25, 2018 at 07:26 AM
I did as you said and it all worked. Thank you! Problem solved
no problem, it gets confusing, but usually if you go 'line by line' through a script, checking what happens to the variables, you can see the problem...it just takes longer..lol
Follow this Question
Related Questions
How to reset a variable? 1 Answer
Pass a function(float) as variable 2 Answers
Transfering Variables Between Sections [SOLVED] 2 Answers