How to Destroy Randomly Generated Objects

Asked 2 years ago, Updated 2 years ago, 49 views

How to destroy randomly generated objects

Objects are randomly generated in an array as shown below.

intenemyIndex=Random.Range(0,m_enemyList.Length);
varenemyObject=Instantiate(m_enemyList[enemyIndex], new Vector3(0,3,0), transform.rotation) as GameObject;

varbackground2 = GameObject.FindGameObjectWithTag("background");
enemyObject.transform.SetParent(background2.transform, false);

I would like to delete the generated object in destroy. Is there a way to destroy the randomly generated object?

c# unity3d

2022-09-30 15:04

1 Answers

You can keep the generated objects in a list and discard them together later, but I thought it would be easier to self-destruct them in the enemy's script because they seem to be enemies.

// Discard when it is no longer needed in enemy scripts
    Destroy(gameObject);

If it's a problem to discard it when it's no longer needed, you can discard it at a specified time.

// Discard after 1 second when no longer needed in enemy script
    Destroy(gameObject, 1.0f);


2022-09-30 15:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.