Why is a function being called for one trigger but not the other?

I am working on a top-down infinite scroller where the player drives up and avoids traffic that is spawned on random locations on the road. The player has a box collider (tagged “PlayerCheck”) that prevents traffic from spawning within view of the player camera. Each instantiated prefab of traffic has a capsule collider (tagged “Traffic”) that is intended to prevent traffic from spawning on top of existing traffic.


The road has an array of spawn points. The road also has a box collider that sits above the spawn points. The road has a spawn manager script (see below). The trigger colliders determine the values of the two boolean variables. If those values are both true, a spawn function is called (NorthSpawnMechanism).


The main issue is that for some reason, the PlayerCheck collider successfully turns off the spawn function, but the Traffic collider does not. The Traffic collider even correctly prints the Traffic Debug.Log messages when Traffic passes through the road spawn point collider. Why is one of my trigger colliders (PlayerCheck) preventing the call of a function while the other (Traffic) does not?


I have tried removing the PlayerCheck and tested the Traffic trigger by itself and still no luck. Thanks in advance for any assistance (first time coder here)!


    bool spawnCarsPlayer = true;
    bool spawnCarsTraffic = true;

    void Update()
    {
        if (spawnCarsPlayer == true && spawnCarsTraffic == true)
        {
            NorthSpawnMechanism();
        }
    }

    void NorthSpawnMechanism()
    {
        if (Time.time > nextSpawnTime)
        {
            nextSpawnTime = Time.time + secondsBetweenSpawns;
            randomNorthSpawnPoint = Random.Range(0, northSpawnPoints.Length);
            Instantiate(NorthTrafficPrefab, northSpawnPoints[randomNorthSpawnPoint].position, Quaternion.identity);
        }
    }

void OnTriggerEnter2D(Collider2D other)
    {
        
        if (other.tag == "PlayerCheck")
        {
            spawnCarsPlayer = false;
            Debug.Log("The player can see the spawn point.");
        }
        

        if (other.tag == "Traffic")
        {
            spawnCarsTraffic = false;
            Debug.Log("There is traffc in the way.");

        }
    }

    private void OnTriggerExit2D(Collider2D other)
    {

        if (other.tag == "PlayerCheck")
        {
            spawnCarsPlayer = true;
            Debug.Log("The player has moved on.");

        }

        if (other.tag == "Traffic")
        {
            spawnCarsTraffic = true;
            Debug.Log("The traffic has moved on.");

        }

    }

Too long question, I’m not sure what happens in your scene, how people could answer ? There are so many questions…


But just in case OnTriggerEnter is called when collision enters a trigger, I did have issue with thngs being already in…

Ok, and I assume traffic spawned by one spawner can cross over into another spawner’s collider space? If so then the first thing I would assume is going wrong is that if two objects are in the spawning trigger and then one leaves it will still set spawnCarsTraffic to true, even if the other object is still in the trigger area. To fix this you can make spawnCarsTraffic an int, and add 1 to it on Trigger Enter and subtract 1 on Trigger Exit, and just check if it is equal to zero before you spawn anything. If this still doesn’t fix the problem then I recommend making the spawnCarsTraffic variable public so you can watch it in the inspector while your game is running and see whether it never works, or only sometimes doesn’t work, or perhaps gets changed back immediately every time it is set to true (or gets incremented if you change to an int)