- Home /
Instantiated objects becomes child of spawn point
When a ship sinks in my game it hits a deadzone. That deadzone calls a random spawnpoint to generate a prefab from the sunken ship and the prefab is a param to the SpawnABoatAtSpawnPosition().
However, an it is not a big deal but I would like to know why, the newly spawned ship is now a child of the spawnpoint. Why?
Here is the Code for the KillZone: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class KillZoneScript : MonoBehaviour
{
public SpawnABoat spawnerOne;
public SpawnABoat spawnerTwo;
public SpawnABoat spawnerThree;
public SpawnABoat spawnerFour;
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "EnemyShip")
{
GameObject DeadShip = collision.gameObject;
SpawnABoat randomSpawnpoint = GetRandomSpawnPoint();
randomSpawnpoint.SpawnABoatAtSpawnpoint(DeadShip);
Destroy(DeadShip);
}
SpawnABoat GetRandomSpawnPoint()
{
SpawnABoat randomSpawnpoint;
int rnd = Random.Range(0, 3);
if(rnd == 0)
randomSpawnpoint = spawnerOne;
else if (rnd == 1)
randomSpawnpoint = spawnerTwo;
else if (rnd == 2)
randomSpawnpoint = spawnerThree;
else
randomSpawnpoint = spawnerFour;
Debug.Log(rnd);
return randomSpawnpoint;
}
}
}
And here is the code for the spawnpoint: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor;
public class SpawnABoat : MonoBehaviour
{
public GameObject myBoat;
public GameObject spawnPosition;
// Start is called before the first frame update
void Start()
{
Instantiate(myBoat, transform.position, transform.rotation);
}
public void SpawnABoatAtSpawnpoint(GameObject myNewBoat)
{
EnemyID thisBoat = myBoat.GetComponent<EnemyID>();
PrefabUtility.InstantiatePrefab(thisBoat.MyPrefab, spawnPosition.transform);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613003935im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawn different gameObjects without using a prefab? | Spawned gameObjects don't enable restartPanel 1 Answer
How to instantiate a projectile only from the weapon prefab of the firing player? 1 Answer
Instantiated clone GameObject spawning at wrong position. 2 Answers
Spawning Player After Player Choice. 1 Answer