Search Unity

[SOLVED] Counter is counting wrong

Discussion in 'Scripting' started by ProExCurator, Feb 19, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Code (CSharp):
    1.    public static int enemyCount;
    2.    public static int enemyEnteredRoom = 0;
    3.    public Text enemyCountText;
    4.    public Text enemyCollisionDetect;
    5. ...
    6. void OnTriggerEnter2D(Collider2D collision)
    7.     {
    8.        if(collision.tag == "RoomC")
    9.        {
    10.            enemyCount++;
    11.            enemyCountText.text = "ENEMY COUNT: " + enemyCount;
    12.            enemyCollisionDetect.text = "ENEMY COLLISION DETECTED";
    13.            enemyEnteredRoom = 1;
    14.        }
    15.    }
    16.  
    17.    void OnTriggerExit2D(Collider2D collision)
    18.     {
    19.        if(collision.tag == "RoomC" && enemyEnteredRoom == 1)
    20.        {
    21.            enemyCount--;
    22.            enemyCountText.text = "ENEMY COUNT: " + enemyCount;
    23.            enemyCollisionDetect.text = "ENEMY COLLISION NOT DETECTED";
    24.            RoomCheckScript.collisionCount = 0;
    25.        }
    26.    }
    When I kill just one enemy, counter is showing -1 value. Why? What I did wrong? Help please.
     
    Last edited: Feb 20, 2020
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi @ProExCurator,

    Do a debug print in your OnTriggerEnter/Exit and see if they fire multiple times, different than what you think they are doing.
     
    ProExCurator likes this.
  3. unit_dev123

    unit_dev123

    Joined:
    Feb 10, 2020
    Posts:
    989
    cast enemyCount toString
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @ProExCurator

    "When I kill just one enemy, counter is showing -1 value. Why? What I did wrong? Help please."

    Proper question would help to understand the problem...

    How do you "kill enemy" ? Do you mean you destroy it?

    Although you didn't show relevant code, I think the issue might with Destroy - if you destroy a GameObject, it registers in OnTriggerExit2D, and your counter could be changed that way.

    You could avoid this by only reducing counter if enemy is still alive (isKilled == false).

    Also it is rather strange to keep book about how many enemies are in a room in enemy; What if you have several rooms? Wouldn't it make more sense to have room keep count on how many enemies it contains? Room has enemies, and enemy is in a room.
     
    ProExCurator likes this.
  5. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @unit_dev123

    "cast enemyCount toString"

    What do you mean by this?
     
    ProExCurator likes this.
  6. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    So yeah... this is what happens at the beginning and this makes sense now... but why this is not showed on "ENEMY COUNT:"?


    but I still don't understand why this is happening.

    I did as you said:
    Code (CSharp):
    1. void Start()
    2.     {
    3.         enemyCount.ToString();
    4. ...
    5.      }
    but how this is supposed to help? Nothing changed after that.

    It looks like that:

    BulletController:
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         if(col.tag == "Enemy" && !isEnemyBullet)
    4.         {
    5.             col.gameObject.GetComponent<EnemyController>().Death();
    6.             Destroy(gameObject);
    7.         }
    8.    }
    So I did this right now:

    EnemyController:
    Code (CSharp):
    1. private bool deadEnemy = false;
    2.  
    3. ...
    4.  
    5. void OnTriggerExit2D(Collider2D collision)
    6.     {
    7.         if(collision.tag == "RoomC" && enemyEnteredRoom == 1)
    8.         {
    9.             if(deadEnemy == true)
    10.             {
    11.                 enemyCount--;
    12.             }
    13.             enemyCountText.text = "ENEMY COUNT: " + enemyCount;
    14.             enemyCollisionDetect.text = "ENEMY COLLISION NOT DETECTED";
    15.             RoomCheckScript.collisionCount = 0;
    16.             Debug.Log("enemyCount: " + enemyCount);
    17.         }
    18.     }
    19.  
    20.     public void Death()
    21.     {
    22.         deadEnemy = true;
    23.         Destroy(gameObject);
    24.     }
    but it doesn't work either, but I quess I did something wrong... again...
     
    Last edited: Feb 19, 2020
  7. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    casting enemyCount to a string won't do anything, I don't know what they are trying to say.

    Are your enemyCount or enemyEnteredRoom modified anywhere else in your game? They are public statics after all.

    Also add some more detailed debug messages. Something like:

    "Object " + gameObject + " has increased/decreased enemyCount to " + enemyCount
     
    ProExCurator likes this.
  8. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    Yes, I was thinking about that, but it shouldn't be modified anywhere else...
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I think you are modifying enemyCount somewhere else. In your original video it looked to me like you were resetting enemyCount to 0 when the doors change to send in the new wave. You were at 2, then when the doors change it goes to -1, which would make sense if you reset it to 0 and on the same frame OnTriggerExit2D is called which subtracts 1, making it -1. Your calculation at this point is off by -2 total, and remains that way throughout the video, always off by -2.

    That's the only place I see the calculation getting off. I think you're going down the wrong rabbit hole looking at OnTriggerExit2D.

    I think what was meant was you're using enemyCount, which is a static int, as a string in several places. C# will try to deal with this by implicitly calling enemyCount.ToString() for you when it thinks you're trying to use it as a string, but it isn't a terrible idea to just put that in your code explicitly when you want the string representation of it. Though it is not the cause of your problem.

    It is just not a great habit to get into passing an int as a string, because not all methods will deal with this situation the same way.
     
    ProExCurator likes this.
  10. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    You are completly right. I think I found the problem, enemyCount is changed to 0 when player enters a room, and I think it counts it after enemy is spawned, but it's a little more complicated now... I need to think how to fix it...

    EDIT: Fixed it, but it would be too long to explain. Thanks everyone anyway ;)
     
    Last edited: Feb 20, 2020
    Joe-Censored likes this.