Need some help with OnTriggerEnter2D

void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == “Player”)
{
Debug.Log(“player is in room!”);

            if (isActive == false)
            {
                activeRooms.Add(gameObject);
                isActive = true;
                Debug.Log("Added room to active room!");
            }
            mapGen.spawn();
        }
    }

Hey there, I need some help with this code. I’m still learning Unity, so please don’t mind the crappy code. In this sample code, I add the game object this script to list that I made from another script. The isActive variable is used to detect if this game object is already in the list. However, Unity seems to ignore that and lot of this code too? When I play this script, the first Debug.Log is displayed in the console, the game object is added to the list, but nothing else here changes. It doesn’t call the function or change isActive to true. I don’t understand, is it something to do with how triggers or lists work or is it just my code? Thanks.

Hello, everything looks fine with your code so far and when the first Debug.Log is called, it also means that you have prepared everything correctly for the actual collision and the tag is recognized.
So my guess is, that there’s something about the line where you’re trying to add the room to the list.

If there is a runtime error then it would also explain why isActive is not set to true .

Try inserting a Debug.Log in front of the line in which you add to the list, or comment out this line and see whether isActive is then set to true in order to isolate the error.

I think I figured it out now! I am an idiot. I forgot that OnTriggerEnter2D should be only added to the game object entering the trigger and that it shouldn’t be on the trigger itself. The reason why the first debug.log would work is because I wrote a OnTriggerEnter2D onto the playerMovement so I can figure out whether it should be applied on the player or the trigger. But then my dumbass forgot that it was the player who triggered it. Sorry for all the confusion and thanks for your help.