[SOLVED]Need help with a weird problem…

Updated on April 10, 2017 in  [R] Scripts
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
4 on April 10, 2017

I’m new to programming so im sry if i wont give enough information to solve my problem,not completely sure what could be doing this.So the problem is the value i put in(its the last line on the code below) “” player.Damage(2); “” it will for some reason be multiplied by 2,so it will reduce my curHealth by 4…

this is not the whole code its just everything apllying to health/dmg(hope i didnt miss anything)

 public int curHealth;
 public int maxHealth = 5;
 
 void Start()
 {
 curHealth = maxHealth;
 }
 void Update()
 {
 if(curHealth > maxHealth)
 {
 curHealth = maxHealth;
 }
 if(curHealth <= 0)
 {
 Die();
 }
 void Die()
 {
 Application.LoadLevel(Application.loadedLevel);
 }
 public void Damage(int dmg)
 {
 if (curHealth < dmg)
 {
 dmg = curHealth;
 }
 curHealth -= dmg;
 }
 

and this is a different cs file(not sure if it matters…)
 private PlayerMain player;
 void Start () {
 player = GameObject.FindGameObjectWithTag("Player").GetComponent();
 }
 void OnTriggerEnter2D(Collider2D col)
 {
 if (col.CompareTag("Player"))
 {
 player.Damage(3);
 }
 }
 }
 

  • Liked by
Reply
3 on April 10, 2017

Do you have more than one collider on the object of the script and/or his children?
Because that can call ‘OnTriggerEnter2D’ twice or more.

on April 10, 2017

ahh tyvm,that was the problem.is it possible to reference a specific collider,since i have a boxcollider and a circle collider.I tried void OnTriggerEnter2D(BoxCollider2D col)  and it threw an error saying i can only use Collider2D

Wise
on April 10, 2017

give the child object a different tag.

on April 10, 2017

the child wasnt the problem i had 2 colliders in the same object,the child luckily didnt trigger it.but thx again i will try to make the 2nd collider an child with a different tag.

Show more replies
  • Liked by
Reply
Cancel