0

Currently I am working on 2D Platformer game and there is a bug that gives me headache. Basicly, if our player, collide to an enemy object he dies. on the other hand if he collect a coin, that means he finished the level. According to this situations, the code actives winPanel, or losePanel. But I dont know why, code only works on lose situations, does not active winPanel.

This part is where I check and identify collisions according to the tags.

 private void OnCollisionEnter2D(Collision2D collision)
    {
       
        if (collision.gameObject.CompareTag("Enemy"))
        {
            GetComponent<TimeControl>().enabled = false;
            Die();
        }

         else if (collision.gameObject.CompareTag("Finish"))
        {
            
            Destroy(collision.gameObject);
            StartCoroutine(Wait(true));
            
        }
    }

Then I am sending true or false to the "Wait" function for enabling win or lose panel.

 IEnumerator Wait(bool win)
    {
       
        yield return new WaitForSecondsRealtime(2f);
        Time.timeScale = 0;

        if (win==true)
        {
            
            winPanel.SetActive(true);
        }

        else
        {
            losePanel.SetActive(true);
        }
    }

The game is perfectly working on lose or die situations, but not working when player collect coin tagged "Finish".

  • does your finish object has a tag called "Finish" ? Add a debug log at the beginning of the collision enter method and print each objects tag that player collides with and you can see what happens when it collides with finish. – Çağatay IŞIK Jan 07 '23 at 12:38
  • @ÇağatayIŞIK Of course my object has a tag "Finish". I checked numerous times. Also, I tried the same thing you said. In console window, I couldn't see the coin object's name. Somehow player does not collide with the coin object. – Berke Tabak Jan 07 '23 at 12:48
  • does your finish object's collider trigger field is tick ? if so it means that you need to check it under OnTriggerEnter2D method – Çağatay IŞIK Jan 07 '23 at 12:52

1 Answers1

0

There are 2 possibilities:

  1. Your coin does not have a "Finish" tag.
  2. Your coin's collider 2D is treigger that means player can run in to it. But in that situation u need to use another method to detect: OnTriggerEnter2D. Here is unity documentation page: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
EFox
  • 38
  • 5