- Home /
how to destroy empty object of children i generated after some move
how to destroy child object after some position reach? i make empty object (and instantiate some prefab) from the position of other empty object and then give the empty obejct force to move. i want to destroy this moved empty object (and their child prefab) after some postion reach, just like some bullet being shoot i need to destroy it or else there will be soo many empty object lying around
here my code
void Update()
{
if (Input.GetKeyUp("space")==true)
{
spawn();
}
if (GetComponentInChildren<Transform>().position.z == 3)
{
Debug.Log("ye");
}
void spawn()
{
GameObject tempempty = new GameObject("basis");
tempempty.AddComponent<Rigidbody>();
tempempty.GetComponent<Rigidbody>().useGravity = false;
tempempty.transform.SetParent(this.transform);
for (int x = 0; x < luas.x; x++)
{
GameObject tempbasisy = Instantiate(basis);
tempbasisy.transform.SetParent(tempempty.transform);
tempbasisy.transform.position = new Vector3(x, 0, 0);
}
tempempty.GetComponent<Rigidbody>().AddForce(0, 0, 100);
}
im sorry i ask this basic question, im new to coding and unity
Answer by logicandchaos · Sep 07, 2021 at 01:07 PM
Destroy(tempbasisy); If you destroy the parent all children are also destroyed. Altho, if you are doing it a lot you might want to use pooling instead.. Sorry, if you need to know when to destroy it:
Vector3 origin;
float destroyDistance;
origin=tempbasisy.transform.position;
if(Distance(tempbasisy.transform.position,origin)>destroyDistance)
Destroy(tempbasisy);
Your answer

Follow this Question
Related Questions
getchild(0), but first child is gonna be destroyed! 1 Answer
How to Destroy Individual Instance of an Object 3 Answers
How To Destroy A Clone? 2 Answers
Destroy() not working on prefab instance 2 Answers