(I am new to unity BTW) So in my game, an object(enemy) is moving from the left of the screen, to the right of the screen, then it de-spawns if the player collides with it. (the player is a hammer-thing that slams down from the top). The enemy is instantiated, so clones then spawn in after it. My problem is that when i destroy an enemy, the enemies stop spawning after it.
Does anyone know a way in which I can destroy the enemy that collided with the player, and clones of the enemies continue to spawn after. Thanks 
2 Answers
2
It’s good practice in game development to keep static classes of things that last for the entire game. For instance your spawning system should not be controlled by a clone, but rather by something that will continue existing. Make a script called GameManager (or whatever) and attach it to something constant like your Main Camera (or whatever). Then in your enemy script, store an instance of GameManager:
public GameManager gameManager;
void Start(){
gameManager = Camera.main.GetComponent<GameManager>(); //Or:
gameManager = GameObject.Find("My Object of choice").GetComponent<GameManager>();
}
Then the other script:
public class GameManager : MonoBehaviour{
public void SpawnEnemy(){
//Enemy spawning goes here. Whenever OnTriggerEvent happens, call this
}
}
Or better yet the advanced option is to create a static class (and place it above your MonoBehaviour class:
public static class GameBehaviour {
public static void SpawnEnemy(){
//Same thing, spawn stuff here
}
}
public class MyDumbMonoBehaviourClassIWontUseShushDontTellMumElseTheMunstersComeForMe : MonoBehaviour{
}
Then in your cloned enemy script you can call GameManager.SpawnEnemy() after trigger event if you’re using static class or just this.SpawnEnemy() if going with the first example.
public class EnemySpawner : MonoBehaviour
{
public GameObject Red;
public GameObject OrangeRed;
public GameObject Orange;
public Transform Spawn1;
public Transform Spawn2;
public Transform Spawn3;
public Camera fpsCam;
private void Start()
{
StartCoroutine(Spawner());
}
// Update is called once per frame
void Update()
{
DetectClickAndDestroy();
}
IEnumerator Spawner()
{
Start:
int num1 = UnityEngine.Random.Range(1, 4);
float diff = Time.timeSinceLevelLoad / 200;
float difficulty = 1 - diff;
Debug.Log(difficulty);
if (num1 == 1)
{
GameObject RedCube;
RedCube = Instantiate(Red, Spawn1);
Destroy(RedCube, 10);
}
else if (num1 == 2)
{
GameObject OrangeRedCube;
OrangeRedCube = Instantiate(OrangeRed, Spawn2);
Destroy(OrangeRedCube, 10);
}
else if (num1 == 3)
{
GameObject OrangeCube;
OrangeCube = Instantiate(Orange, Spawn3);
Destroy(OrangeCube, 10);
}
yield return new WaitForSeconds(difficulty);
goto Start;
}
My spawner here generates a random number and spawns a type of enemy depending on the number. These enemies are clones of enemy prefabs/gameobjects that I assign in the inspector. They spawn faster over time and always spawn at designated spawn points. I have another function which destroys the specific clone that I click on as well but not in the script below. Hope it helps.
@Weaver13, in the code example in the middle, it says that the spawn function will be called when there is a collision, but I'd like the enemies to keep spawning after a collision, basically to infinitely spawn.(I want the enemy that was hit to de-spawn, but the problem is enemies stop spawning after a collision with a player.
– namaninja43