how to replace 5 in yeild WaitForSeconds(5) with a variable

i am trying to spawn prefabs at random time intervals and for some reason the script makes my unity hang

so my question is…

how to replace 5 in yeild WaitForSeconds(5) with a variable in the following script?

var pickup : Transform;

var d = Random.Range(5,20);

private var curObject: Transform;

function Start () 
{
    while (true)
    {   
        var y = Random.Range(-4,6);
                                                            
        var prefab;
            
        prefab = pickup;
            
        curObject = Instantiate(prefab, Vector3(20,y,0), Quaternion.identity);
    }
        
    yield WaitForSeconds(d); 
}

thank you.

Your yield is outside of the while(true). It should be right after “curObject = …”.

[Edit] Also, the random is sampled only once at start, which mean that those objects will be instantiated at a regular interval (but a different one at each execution)