destroy a specific instantiated prefab

so im making aimlabs kind of game where all i have to do is click on an object an it is destroyed.
so far, i have made the balls be able to spawn.

public GameObject spherePrefab;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
            Instantiate(spherePrefab,randomSpawnPosition,Quaternion.identity);
        }
    }

problem is even if i use the code in another script Destroy(). idk what gameobject to put the script inside since the object doesnt show up in my hierarchy until im in the game spawning it. i followed this tutorial and tbh i havent fully grasped it yet.

public GameObject spherePrefab;
void OnMouseOver()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Destroy(spherePrefab);
        }
    }

This is what i have so far in another script. even if i get this script down i dont really have an object to put it under ;-;
There will also be another problem that i suspect will be related to this, but just to point it out specifically since i will need it later, how do i reference the individual balls directly, since in the future i want to make it so that only three balls are on screen at once, and that it cant spawn more when it detects three.

You can use OnMouseDown() function to know if you have pressed something with your mouse1(left click)
So making your second script like this

private void OnMouseDown()
{
    Destroy(transform);
}

and putting it in your sphereprefab might help.

If you put this in your sphereprefab it will delete the gameobject itself by using Destroy(transform). And you can use this to create and destroy multiple spheres

But for OnMouseDown to work you must have a collider in your sphereprefab

how would i put this in my sphereprefab if it isnt an object in my hierarchy? it only appears in my hierarchy when i am in the game already spawning them, it will appear as (name)(clone). and also i only want to destroy one sphereprefab at a time since i can only click on one sphereprefab at one time.

Select your sphereprefab and select add component. Select the script you have created and and that will be enough. After that whenever your prefab is clicked it will destroy itself.

9076675--1256164--upload_2023-6-13_10-54-54.png

A little side note if you are not using your prefab from a folder but in the hierarchy you might destroy your main prefab and the game wont be able to instantiate more of your sphereprefab. Either use a prefab from your folders or disable your main prefab that you are instantiating in your hierarchy

Hey! thank you so much it worked! :smile:
but i do have a question to learn about this just abit more. what do you mean by a prefab from a folder and not in the hierarchy? by folder do you mean like, from assets folder?
another question is, how would i go around referencing the prefab? do i jus give them a tag?
because if so, i have tried this

    public GameObject spherePrefab;

    // Update is called once per frame
    void Update()
    {
        if (GameObject.FindGameObjectsWithTag("targettag").Length > 3)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
                Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
            }
        }
    }

and well, doesnt seem to work? when i press space it just doesnt spawn at all

1 Like

Yes if you just drag your prefab into a public GameObject you can use it. This (from my experince) mostly used for Instantiating objects
9076801--1256173--upload_2023-6-13_11-43-15.png.
As you can see in my inspector there is a blocker object that is taken from my hierarchy and Environments that is taken from my folders as a prefab.

Yes you can give them tags or names to tell them apart. Here is how I would do this with tags

if (Input.GetKeyDown(KeyCode.Space))
{
    Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    clone.tag = "spherePrefab"
}

Here is how I would do it with names

int counter;

if (Input.GetKeyDown(KeyCode.Space))
{
    Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    clone.name = $"sphere{counter}";
    counter++;
}

This would make it so every object you create using instantiate would be a diffirent name (sphere0,sphere1,sphere2…) so you can use things like Find() to search a spesific clone

BTW I would recommend giving your instantiated objects to something else like I did here with var clone to more easily access them while creating them

Edit: You can find and destroy all of these clones easily by using this code if you used tags

var clones = GameObject.FindGameObjectsWithTag("spherePrefab");
foreach (var clone in clones)
{
    Destroy(clone);
}

i want to make it so that at any given point, the number of balls can only be maximum 4. But using your method, while i can limit to 4 balls. once i start destroying them, they cannot spawn anymore because the counter already reached 4.

    void Update()
    {
        if (counter < 4)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
                var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
                clone.name = $"sphere{counter}";
                counter++;
            }
        }
    }

I tried to solve this by adding this to the script where i register the click

    int count = RandomSpawner.counter;
    public GameObject spherePrefab;
    void OnMouseOver()
    {
        if(Input.GetMouseButtonDown(0))
        {
            Destroy(spherePrefab);
            count -= 1;
        }
    }

but alas it did not work. the result is still the same, when i go into the game and spawn 4 balls, once i click and destroy them, i cannot spawn more.

Sorry for the late response but I added that coutner just to name the clones as an example. You don’t need to use that as the main counter. So lets change the code like this.

[SerializeField] private GameObject spherePrefab;
    private int nameCounter;
    public int activeCloneCounter;

    void Update()
    {
        if (activeCloneCounter < 4)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
                var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
                clone.name = $"sphere{nameCounter}";
                nameCounter++;
                activeCloneCounter++;
            }
        }
    }

Now with this your sphere name counter is diffirent and your active sphere counter is diffirent. With this you can have infinite amount of sphere names and maximum of 4 spheres active on the game. Now lets get to your other script that destroys the balls when we click. Make it so its like this

private SphereSpawner sphereSpawner;
    void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Destroy(spherePrefab);
            SphereSpawner.activeCloneCounter--;
        }
    }

Now in this code you need to put your first code that spawns the spheres. I dont know what your script name is so I put “SphereSpawner” you need to change this. And than add your first script in to the prefab of your sphere so you can change the activeCloneCounter variable from another script. By doing this you can add multiple spheres with a maximum of 4 and when they get destroyed it will let you add more spheres.

ps. if you want to fully reset your scene dont forget to destroy all the clones and making activeCloneCounter = 0.

yoo this helped me so much thanks man