Gameobjects do not get parented properly, double gameobjects after instanciating
I am trying to spawn Units (Prefabs) and parent them to an empty gameobject and destroy the old one beforehand. Destroying works just fine. I have two Problems here. First i get a second empty game object (not named (clone)) and i get an aditional Unit which is named like my prefab although i rename them. My second Problem is just one Unit gets parented to the empty game object. The rest is unparented in the hierarchy.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class RegimentSpwan : MonoBehaviour
{
public Text row;
public Text unitAmmount;
public GameObject Unit;
private GameObject _newGO;
public void OnclickNewRegiment()
{
Destroy(GameObject.Find("Regiment"));
_newGO = new GameObject("Regiment");
Instantiate(_newGO, this.transform.position, Quaternion.identity);
for (int i = 0; i < Convert.ToInt16(row.text); i++)
{
for (int j = 0; j < Convert.ToInt16(unitAmmount.text); j++)
{
Instantiate(Unit, this.transform.position, Quaternion.identity);
Unit.name = "Unit_" + i + "_" + j;
Unit.transform.parent = _newGO.transform;
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612093530im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
vehicle/gameobject moving/listing/rotating when made parent of main character (child) game object 1 Answer
Why is transform.parent not working in Unity 5.5.4? 0 Answers
Canvas as a Child with Screen Overlay follows parent scale 0 Answers
Error with parenting spawned weapon on client side 0 Answers
How to attach an object to another object so that moving one moves the other 0 Answers