- Home /
GetChild(0) returns nonexistent object
I have empty game object with that has child (Cube). In a script attached to empty object which destroys its child and replaces it with new one (Sphere) after that GetChild(0) should return "Sphere(clone)" but it returns "Cube" despite object was replaced and it shows Sphere(clone) in the hierarchy editor.
public class TestTest : MonoBehaviour
{
[SerializeField] private GameObject newChild; // <- sphere
void Update()
{
if (Input.GetButtonDown("Fire2"))
{
Debug.Log(transform.GetChild(0).name); //returns cube
Destroy(transform.GetChild(0).gameObject);
Instantiate(newChild, transform);
Debug.Log(transform.GetChild(0).name); //STILL returns cube
}
}
}
Answer by ShadyProductions · Jun 12, 2020 at 02:37 PM
try DestroyImmediate
Woah it worked thank you. How do i mark this reply as correct answer?
Your answer
Follow this Question
Related Questions
Destroying childs and Instantiate [C#] 0 Answers
Delete children of Instantiated object 2 Answers
Instantiate script for randomly selecting sound is suddenly creating New Game Object in hierarcy?? 2 Answers
How to destroy all gameobjects active in hierarchy? 1 Answer
Destroy temp object and make new object. 0 Answers