I’ve been scratching my head for days trying to implement this.
essentially I just want something that will spawn a pellet (little red ball) for the player to enjoy. the game runs fine without this script running.
I disable
public bool spawning;
and it works fine, I enable it my unity stops responding and crashes, I know I’m doing something wrong, I just don’t know what. here’s the full script, can someone be kind enough to point me in the right direction to fix this?
thank you very much in advance.
using UnityEngine;
using System.Collections;
public class PelletSpawn : MonoBehaviour {
public GameObject pellet;
public float spawnDelaymin, spawnDelaymax;
private float waitTime;
public bool spawning;
// Use this for initialization
void Start () {
//Defines starting pause before spawning pellets
waitTime = Random.Range (spawnDelaymin, spawnDelaymax);
//start the pellet spawning
StartCoroutine (Spawn ());
}
void FixedUpdate () {
//Randomizes the wait time each "spawn cycle" persay...
//I think this is what is crashing my unity...
//tried removing it yet it doesn't stop it from crashing.
waitTime = Random.Range (spawnDelaymin, spawnDelaymax);
}
//While spawn is enabled,
//spawn a pellet and wait for a new cycle
//FixedUpdate() regenerates new waitTime for random spawn rates...(?)
IEnumerator Spawn(){
while (spawning) {
DropPellet ();
yield return new WaitForSeconds (waitTime);
}
}
//This function is also needed elsewhere later, not implemented yet
void DropPellet(){
Instantiate (pellet);
}
}