The is the reason you were freezing you are basically in an endless loop in the while block. What you need is to add a yield statement inside the while loop.
You can fix the freezing like this
while (true)
{
chooser = Random.Range (1, 5);
sec = Random.Range (1, 5);
if (chooser == 1)
{
Instantiate (obj1, transform.position, Quaternion.identity);
}
if (chooser == 2)
{
Instantiate (obj2, transform.position, Quaternion.identity);
}
if (chooser == 3)
{
Instantiate (obj3, transform.position, Quaternion.identity);
}
if (chooser == 4)
{
Instantiate (obj4, transform.position, Quaternion.identity);
}
if (chooser == 5)
{
Instantiate (obj5, transform.position, Quaternion.identity);
}
// this is the reason you were freezing
// you need a yield statement inside the while loop
yield return new WaitForSeconds(0.01f);
}
But, if you do not mind the suggestion
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class spawner : MonoBehaviour
{
public List<GameObject> objects = new List<GameObject>();
public bool spawned;
public float chooser;
public float sec;
void Start ()
{
if (objects.Count > 0)
{
StartCoroutine (spawning ());
chooser = Random.Range (0, 5);
sec = Random.Range (0, 5);
}
}
// Update is called once per frame
void Update ()
{
// because of the while statement the following is not needed
// StartCoroutine (spawning ());
}
IEnumerator spawning()
{
while (true)
{
// For integers, Random.Range(0, 5) will return a value of 0-4
chooser = Random.Range (0, 5);
sec = Random.Range (0, 5);
if (chooser < objects.Count)
{
Instantiate (objects[chooser], transform.position, Quaternion.identity);
}
// this is the reason you were freezing
// you need a yield statement inside the while loop
yield return new WaitForSeconds (2);
}
yield return 0;
}
}
This is an infinite loop. There’s no condition to stop looping, so it just keeps going. Also there is no yield inside of the loop, so the game is going to hang.
Remove “spawning()” from Update function, because that’s not how coroutines are started.
Either change the while() so it has a proper condition and/or move the yield to inside of the loop.