Script crashing unity.

I’m trying to create asteroids that spawn from placed empty objects in the scene however the below script for which to instantiate them at random intervals is crashing the unity editor.

any clues as to why? and how I could solve this issue?

Code:


    using UnityEngine;
    
    using System.Collections;
    
    public class asteroidBehaviour : MonoBehaviour {
    	public Rigidbody asteroid;
    	public float speed=1500f;
    	
    	// Use this for initialization
    	void Start () {
    		SpawnAsteroid();
    	}
    	
    	 IEnumerator wait() {
            yield return new WaitForSeconds(Random.Range(10.0f, 15.0f));
        }
    	
    	void SpawnAsteroid(){
    		while(true){
    			wait();
    		Rigidbody instance = Instantiate(asteroid, transform.position, transform.rotation) as Rigidbody;
    		transform.RotateAround(instance.position,Vector3.up, 20*Time.deltaTime);
    		
    		Vector3 motion = transform.InverseTransformDirection(Vector3.forward);
    		instance.AddForce(motion *speed);
    	}
    	}
    	// Update is called once per frame
    	void Update () {
    		
    		
    	}
    }

Thanks,

Euden

Here is a bit of rewriting of your code. Your main issue was how you were trying to use IEnumerator and yield:

using UnityEngine;
using System.Collections;

public class AsteroidBehaviour : MonoBehaviour {
	public Rigidbody asteroid;
	public float speed=1500f;
	
	void Start () {
		StartCoroutine(SpawnAsteroid());
	}

	IEnumerator SpawnAsteroid(){
		while(true){
    		yield return new WaitForSeconds(Random.Range(10.0f, 15.0f));
    		Rigidbody instance = Instantiate(asteroid, transform.position, transform.rotation) as Rigidbody;
    		transform.RotateAround(instance.position,Vector3.up, 20*Time.deltaTime);
    		
    		Vector3 motion = transform.InverseTransformDirection(Vector3.forward);
    		instance.AddForce(motion *speed);
			}
	}
}