- Home /
How do I delete a certain number a clones from my scene?
Hi, I'm veeery new to game development and C#, and I was wondering how I can delete a certain number of clones from my scene. I'm making a tycoon-like game where points are symbolized by cubes and when you buy an upgrade the cubes (points) that you spent disappear. Heres a snippet of my code:
//Adds to score and gives the user a cube
if (Input.GetKeyDown(KeyCode.Mouse0))
{
Instantiate(cubePrefab, new Vector3(0, 10, 0), Quaternion.identity);
score++;
Debug.Log(score);
}
//Gives the user a golden cube but subtracts 50 from score
if (Input.GetKeyDown(KeyCode.Mouse1) && score >= goldCost)
{
Instantiate(goldenCubePrefab, new Vector3(0, 10, 0), Quaternion.identity);
score = (score - goldCost);
goldCubes = (goldCubes + 1);
Debug.Log(score);
StartCoroutine(AddGoldScoreRoutine());
//Destroys 50 regular cubes
Destroy(GameObject.Find("cube(Clone)"), 50);`
Not sure if "Destroy(GameObject.Find("cube(Clone)"), 50)" is the correct method or I'm doing something wrong, because whenever I "Buy" something the cubes don't get removed from the scene. Does anyone have a solution?
Answer by FlaSh-G · Apr 02, 2020 at 08:27 PM
The second parameter of the Destroy method is a delay in seconds that Unity will wait until destroying the one (!) object you passed. GameObject.Find returns a reference to one object (or null
if none is found). If you want to destroy 50 objects, you have to find those 50 objects and Destroy each of them.