- Home /
Other
How to break a prefab connection via c#
Hello. So I am instantiating this prefab into the world, but the problem is they are all connected. I want them to act like they are all a separate object, but can't find out how. So this way when I destroy an object that was spawned into the scene, they don't all get destroyed. I hope this makes sense. I am coding in c#, here it is:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemySpawner : MonoBehaviour {
 [SerializeField] float secondsBetweenSpawns;
 [SerializeField] GameObject enemyToSpawn;
 [SerializeField] int enemiesInScene;
 [SerializeField] int desiredEnemiesInScene;
 [SerializeField] Transform Parent;
 [SerializeField] PathFinder pathFinder;
 void  Start() {
     StartCoroutine(Spawner());
 }
  IEnumerator Spawner()
  {
    while (enemiesInScene < desiredEnemiesInScene)
    {
         yield return new WaitForSeconds(secondsBetweenSpawns);
         print("Spawning Enemy");
         Spawn(); 
         enemiesInScene = enemiesInScene + 1;
         print(enemiesInScene);
     }
    if (enemiesInScene == desiredEnemiesInScene)
     {
         // do nothing
     }
 }
 void Spawn()
 {
     float whereToSpawnEnemyX = pathFinder.startingWaypoint.transform.position.x;
     float whereToSpawnEnemyZ = pathFinder.startingWaypoint.transform.position.z;
     Vector3 whereToSpawnEnemy = new Vector3(whereToSpawnEnemyX, 20f, whereToSpawnEnemyZ);
     GameObject spawnedInEnemy = Instantiate(enemyToSpawn, whereToSpawnEnemy, Quaternion.Euler(0, 90, 0));
     spawnedInEnemy.transform.parent = Parent;
 }
}
Follow this Question
Related Questions
How do i Instantiate a prefab with specific assests included 3 Answers
Distribute terrain in zones 3 Answers
how Load Prefabs after build 0 Answers
How do I destroy a Instantiated UI image that is pushed on the Canvas? 2 Answers
Possible Bug? GameObject.Instantiate copies instance instead of Prefab 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                