Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

destroy a specific instantiated prefab

Discussion in 'Getting Started' started by jermainetai0307, Jun 12, 2023.

  1. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    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.
    Code (CSharp):
    1. public GameObject spherePrefab;
    2.  
    3.     // Update is called once per frame
    4.     void Update()
    5.     {
    6.         if (Input.GetKeyDown(KeyCode.Space))
    7.         {
    8.             Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    9.             Instantiate(spherePrefab,randomSpawnPosition,Quaternion.identity);
    10.         }
    11.     }
    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.
    Code (CSharp):
    1.  
    2. public GameObject spherePrefab;
    3. void OnMouseOver()
    4.     {
    5.         if(Input.GetMouseButtonDown(0))
    6.         {
    7.             Destroy(spherePrefab);
    8.         }
    9.     }
    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.
     
    Last edited: Jun 12, 2023
  2. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    You can use OnMouseDown() function to know if you have pressed something with your mouse1(left click)
    So making your second script like this
    Code (CSharp):
    1. private void OnMouseDown()
    2. {
    3.     Destroy(transform);
    4. }
    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
     
  3. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    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.
     
  4. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    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.

    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
     
  5. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    Hey! thank you so much it worked! :D
    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
    Code (CSharp):
    1.     public GameObject spherePrefab;
    2.  
    3.     // Update is called once per frame
    4.     void Update()
    5.     {
    6.         if (GameObject.FindGameObjectsWithTag("targettag").Length > 3)
    7.         {
    8.             if (Input.GetKeyDown(KeyCode.Space))
    9.             {
    10.                 Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    11.                 Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    12.             }
    13.         }
    14.     }
    and well, doesnt seem to work? when i press space it just doesnt spawn at all
     
    Last edited: Jun 13, 2023
    Hexolenn likes this.
  6. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    Yes if you just drag your prefab into a public GameObject you can use it. This (from my experince) mostly used for Instantiating objects
    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
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space))
    2. {
    3.     Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    4.     var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    5.     clone.tag = "spherePrefab"
    6. }
    Here is how I would do it with names
    Code (CSharp):
    1. int counter;
    2.  
    3. if (Input.GetKeyDown(KeyCode.Space))
    4. {
    5.     Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    6.     var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    7.     clone.name = $"sphere{counter}";
    8.     counter++;
    9. }
    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
    Code (CSharp):
    1. var clones = GameObject.FindGameObjectsWithTag("spherePrefab");
    2. foreach (var clone in clones)
    3. {
    4.     Destroy(clone);
    5. }
    6.  
     
    Last edited: Jun 13, 2023
  7. jermainetai0307

    jermainetai0307

    Joined:
    Nov 10, 2022
    Posts:
    27
    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.
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if (counter < 4)
    4.         {
    5.             if (Input.GetKeyDown(KeyCode.Space))
    6.             {
    7.                 Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    8.                 var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    9.                 clone.name = $"sphere{counter}";
    10.                 counter++;
    11.             }
    12.         }
    13.     }
    I tried to solve this by adding this to the script where i register the click
    Code (CSharp):
    1.     int count = RandomSpawner.counter;
    2.     public GameObject spherePrefab;
    3.     void OnMouseOver()
    4.     {
    5.         if(Input.GetMouseButtonDown(0))
    6.         {
    7.             Destroy(spherePrefab);
    8.             count -= 1;
    9.         }
    10.     }
    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.
     
  8. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    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.

    Code (CSharp):
    1. [SerializeField] private GameObject spherePrefab;
    2.     private int nameCounter;
    3.     public int activeCloneCounter;
    4.  
    5.     void Update()
    6.     {
    7.         if (activeCloneCounter < 4)
    8.         {
    9.             if (Input.GetKeyDown(KeyCode.Space))
    10.             {
    11.                 Vector3 randomSpawnPosition = new Vector3(Random.Range(-30, 31), Random.Range(0, 25), Random.Range(0, 41));
    12.                 var clone = Instantiate(spherePrefab, randomSpawnPosition, Quaternion.identity);
    13.                 clone.name = $"sphere{nameCounter}";
    14.                 nameCounter++;
    15.                 activeCloneCounter++;
    16.             }
    17.         }
    18.     }
    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
    Code (CSharp):
    1. private SphereSpawner sphereSpawner;
    2.     void OnMouseOver()
    3.     {
    4.         if (Input.GetMouseButtonDown(0))
    5.         {
    6.             Destroy(spherePrefab);
    7.             SphereSpawner.activeCloneCounter--;
    8.         }
    9.     }
    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.
     
    Last edited: Jun 16, 2023