Code works 50% of the time

I have two codes but sometimes the enemys are spawning and sometimes not

using System.Collections;//moet het doen
using System.Collections.Generic;
using UnityEngine;

public class Teleportation : MonoBehaviour {

    public GameObject Portal;
    public GameObject Player;
    private Transform tf;
    private bool canRotate;

    public int tpDelay;
    public float rotationSpeed = 5f;
    private GameObject pentagram;
    private GameObject boss;
   
    void Start()
    {
        pentagram = GameObject.FindWithTag("Pentagram");
        tf = pentagram.GetComponent<Transform>();
        canRotate = false;
        boss = GameObject.FindWithTag("Boss");
    }
    private void Update()
    {
        if(canRotate == true)
        {
            Rotate();
        }
       
    }
    void Rotate()
    {
        tf.Rotate(new Vector3(0f, 0f, 6f*rotationSpeed * Time.deltaTime));
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "Player")
        {
            StartCoroutine (Teleport ());
            canRotate = true;
            boss.GetComponent<Bossspawner>().stop = false;
        }
    }

   

    IEnumerator Teleport()
    {
        yield return new WaitForSeconds (tpDelay);
        Player.transform.position = new Vector2 (Portal.transform.position.x, Portal.transform.position.y);
        canRotate = false;
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bossspawner : MonoBehaviour
{
    public GameObject[] enemies;
    public Vector3 spawnValues;
    public float spawnTime;
    public float spawnTimeHigh;
    public float spawnTimeLow;
    public int startWait;
    public bool stop;

    int randomEnemy;

    void Start ()
    {
        StartCoroutine(waitSpawner());
        stop = true;
    }

    void Update ()
    {
        spawnTime = Random.Range (spawnTimeLow, spawnTimeHigh);
    }

    IEnumerator waitSpawner ()
    {
        yield return new WaitForSeconds (startWait);

        while (!stop)
        {
            randomEnemy = Random.Range (0, 2);
            Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 0, 0);
            Instantiate(enemies[randomEnemy], spawnPosition + transform.position, transform.rotation);
            yield return new WaitForSeconds (spawnTime);
        }
    }
}

Are you getting any errors? Maybe your spawnTimeHigh is too high (remember it’s seconds, so easy to think its not working). Add a bunch of Debug.Log lines all over and you’ll see which code is executing then you can determine what’s not happening where you expect it to be and debug from there.

My guess would be your

  void Update ()
    {
        spawnTime = Random.Range (spawnTimeLow, spawnTimeHigh);
    }

Is constantly updating. maybe you should try putting this code in the Start() ?
I don’t see anything that resets or randomizes these variables so I’m assuming they are going to stay consistent anyway.

If anything try this:

// add this variable
private bool canStartRoutine = true;

void Update()
{
if(canStartRoutine == true)
{
StartCoroutine(waitSpawner());
stop = true; // Maybe you want this in Start() ? You make the call (Personally, I don't think you need it)
}


}

Then for your IEnumerator you want to do this:

 IEnumerator waitSpawner ()
    {
        canStartRoutine = false;
        yield return new WaitForSeconds (startWait);
            randomEnemy = Random.Range (0, 2);
            Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 0, 0);
            Instantiate(enemies[randomEnemy], spawnPosition + transform.position, transform.rotation);
            yield return new WaitForSeconds (spawnTime);
            canStartRoutine = true;
    }

I don’t even understand where the stop was being updated so I removed that and the while loop. You can accomplish the same thing without that mess.

Here’s a better glimpse of the code that I’m advising.

public GameObject[] enemies;
    public Vector3 spawnValues;
    public float spawnTime;
    public float spawnTimeHigh;
    public float spawnTimeLow;
    public int startWait;
 
private bool canStartRoutine = true;

void Start ()
    {
        spawnTime = Random.Range (spawnTimeLow, spawnTimeHigh);
    }

void Update()
{

if(canStartRoutine == true)
{
StartCoroutine(waitSpawner());
}

}

IEnumerator waitSpawner ()
    {
        canStartRoutine = false;
        yield return new WaitForSeconds (startWait);
            randomEnemy = Random.Range (0, 2);
            Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 0, 0);
            Instantiate(enemies[randomEnemy], spawnPosition + transform.position, transform.rotation);
            yield return new WaitForSeconds (spawnTime);
            spawnTime = Random.Range(spawnTimeLow, spawnTimeHigh);
            canStartRoutine = true;
    }