Instantiating objects to create an infinite level.

I have a main object that has a North, West, South and East object as children, ie the name of each object is N,W,S,E. I want the main object to be instantiated and put right in front of me starting with the respective end(when I am facing and going north and I reach the North-object, the main object should be instantiated so I enter South of the just instanced main object etc.). Also, it should remove all instantiated main objects being distant more than 4 instances of the main object of the player.

My code is this and the problems are that rooms are copied randomly. Going back creates new instances in the same place or even different places, sometimes I get null exceptions for Destroy.

What to change:

void OnTriggerEnter(Collider other)
    {
        //N:Z=1
        //O:X=1
        //W:X=-1
        //S:Z=-1
        if (other.gameObject.name == "N" && transform.forward.z > 0.8f)
        {
            Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z + 8), Quaternion.identity);
        }
        if (other.gameObject.name == "S" && transform.forward.z < -0.8f)
        {
             Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x, other.gameObject.transform.position.y, other.gameObject.transform.position.z - 6), Quaternion.identity);
        }
        if (other.gameObject.name == "W" && transform.forward.x < -0.8f)
        {
             Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x - 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
        }
        if (other.gameObject.name == "E" && transform.forward.x > 0.8f)
        {
             Instantiate(Raum.gameObject, new Vector3(other.gameObject.transform.position.x + 6, other.gameObject.transform.position.y, other.gameObject.transform.position.z), Quaternion.identity);
        }
        Raeume = GameObject.FindGameObjectsWithTag("Raum").ToList();
        if (Raeume.Count > 3 && Raeume.First() != null)
            Destroy(Raeume.First());
        //Debug.Log(other.gameObject.name);
    }

At a guess I’d say it’s because your box colliders are triggering more than once on the edges. You’ll get double copies of things and have leftovers where you don’t expect. Also, the null reference exception would be because Destroy doesn’t work until the end of the frame but since it was called on the same GameObject in those two OnTriggerEnters one of them is left with null to destroy. I’d recommend keeping local reference variables for your current directional GameObjects and comparing that way. Alternatively, you could use a bool to check if the OnTriggerEnter method has been triggered recently.