So I’m spawning clouds. I want them to spawn at a much slower speed when my character is standing still, and then spawn faster when he’s running, to keep the sky from filling with clouds when you’re standing still. My code for spawning actually works just fine. The problem is I’m not able to change spawnMax and spawnMin while the level is running. Actually, debug shows me the values are changing, but the Invoke command seems to ignore them. It won’t even let me change the value in the Inspector as the level is running. Is there a better way to do this?
using UnityEngine;
using System.Collections;
public class CloudSpawn : MonoBehaviour
{
public GameObject player;
public GameObject[] cloud;
public float spawnMin = 25f;
public float spawnMax = 35f;
// Use this for initialization
void Start ()
{
SpawnCloud();
player = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update ()
{
if(player.rigidbody2D.velocity.x > 0)
{
spawnMin = 1f;
spawnMax = 3f;
Debug.Log("SpawnMin: " + spawnMin + " " + "SpawnMax: " + spawnMax);
}
else
{
spawnMin = 25f;
spawnMax = 35f;
Debug.Log("SpawnMin: " + spawnMin + " " + "SpawnMax: " + spawnMax);
}
}
void SpawnCloud()
{
Instantiate(cloud[Random.Range(0, cloud.GetLength(0))], transform.position, Quaternion.identity);
Invoke("SpawnCloud", Random.Range(spawnMin, spawnMax));
}
}