- Home /
Hi my spawning stop once the original sprite has been destroyed (The first thing that gets killed). What can I do to avoid this problem?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class enemySpawing : MonoBehaviour { public GameObject enemy; public float respawnTime = 1.0f; private Vector2 screenBounds;
// Use this for initialization
void Start()
{
screenBounds = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
StartCoroutine(enemySpawning());
}
private void spawnEnemy()
{
GameObject a = Instantiate(enemy) as GameObject;
a.transform.position = new Vector2(screenBounds.x * -2, Random.Range(-screenBounds.y, screenBounds.y));
}
IEnumerator enemySpawning()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
spawnEnemy();
}
}
}
How do you spawn your objects? I assume its a script on the original sprite, probably within the update() function. If so, then after deleting your original, unity stops calling the update() function. If not, then please update your script, as you are destroying "enemy.gameobject", instead of the actual collision object "other".
Answer by frederikedel · Oct 22, 2020 at 11:07 AM
@JustACode Really hard to see what you mean by that, you didn't provide the spawner script. But: Whats the point of Disabling the enemy gamobject after you just destroyed it? 2. Why are you destroying enemy, not the object that enters the trigger (other). So please edit your question so somebody can help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220613014217im_/https://answers.unity.com/themes/thub/images/avi.jpg)