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