Hi there I am trying to disable/enable spawning on mouse click but does not seem to work? On the inspector it is unchecked when using the mouse down script which is working fine but the spawn script still works and spawns enemies!
I know that GetComponent ().enabled = false; only stops update and similar functions but how do I stop void Spawn function? My spawn script is also below if needed. Thank you.
void OnMouseDown ()
{
GameObject[] gos = GameObject.FindGameObjectsWithTag ("EditorTouch");
foreach (GameObject go in gos) {
go.GetComponent<Spawner> ().enabled = false;
}
}
}
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public float spawnTime = 5f; // The amount of time between each spawn.
public float spawnDelay = 3f; // The amount of time before spawning starts.
public GameObject[] enemies; // Array of enemy prefabs.
void Start ()
{
// Start calling the Spawn function repeatedly after a delay .
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
void Spawn ()
{
// Instantiate a random enemy.
int enemyIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[enemyIndex], transform.position, transform.rotation);
// Play the spawning effect from all of the particle systems.
foreach(ParticleSystem p in GetComponentsInChildren<ParticleSystem>())
{
p.Play();
}
}
}