- Home /
[Solved] Disable/enable Void Spawn Function?
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();
}
}
}
Seem to be getting close.
First button activates spawn script to start spawning enemies.
void On$$anonymous$$ouseDown ()
{
GameObject[] gos = GameObject.FindGameObjectsWithTag ("EditorTouch");
foreach (GameObject go in gos) {
go.GetComponent<Spawner> ().enabled = true;
go.GetComponent<Spawner>().InvokeRepeating("Spawn");
}
second button deactivates spawn script and cancels invoke spawn but now having trouble activating it again with the first button. I believe not using invokerepeating to activate it again incorrectly..as get " No overload for method InvokeRepeating' takes
1' arguments" error.
void On$$anonymous$$ouseDown ()
{
GameObject[] gos = GameObject.FindGameObjectsWithTag ("EditorTouch");
foreach (GameObject go in gos) {
go.GetComponent<Spawner> ().enabled = false;
go.GetComponent<Spawner>().CancelInvoke("Spawn");
}
Does cancel invoke make it so that I cant start spawn invoke again? Please help.
Answer by G-Jewel · Mar 10, 2015 at 04:39 PM
I solved it by just calling the start function again by script. Thanks for the views I guess. :D
go.GetComponent<Spawner>().Start();
Your answer
![](https://koobas.hobune.stream/wayback/20220613185612im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
disable collision on an object when pressing Q (3D) 1 Answer
Can't disable/enable GameObject Component (C#) 1 Answer
how to make my component disabled after an animation 1 Answer
How to find a component and disable it from another objects script? 2 Answers
uGUI element component names? 4.6 Beta 2 Answers