N
N
NickName73312018-03-16 00:54:53
C++ / C#
NickName7331, 2018-03-16 00:54:53

C#(Unity) - Why is int not incremented?

There are two classes, the first class is the parent class, and it has an int, and a method in which this int is incremented. In the second class, the method is called, and according to the plan, int should increase, but nothing comes out. Here is the code:

public int Triger = 0;

public void GetDamage ()
{
    hp--;
     if (hp == 0) //P.S. Объект не один, поэтому должно работать не один раз
            {
    Trigger++;
    Destroy (gameObject);
            }
}

//Второй класс
  void OnTriggerEnter2D(Collider2D collider)
  {
    Unit unit = collider.GetComponent<Unit>();

    if (unit && unit is Character)
    {
      if (Mathf.Abs(unit.transform.position.x - transform.position.x) < 0.3F) GetDamage();
      else unit.ReceiveDamage();
    }

Where is the mistake? Thanks in advance.
PS This is not a complete code, if you need a complete one, I'll change it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2018-03-16
@NickName7331

If GetDamage is part of MonoBehaviour (which it appears to be), then it's a little unclear where you expect the variable to increment. You destroy the entire GO, along with the script instance.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question