Search Unity

I am not sure anyone can answer this please?

Discussion in 'General Discussion' started by truthseeker089, Jun 13, 2019.

  1. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    hi guys is there any way you can destroy a certain number of objects with the same tag without destroying the rest? it seems to me that the only option is find with tag/name which will destroy ALL of same tags,,thanks!
     
  2. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    Of course there is. The first one that comes to mind is to add them to a list, pick some random number for index and delete a few times as needed.
     
  3. truthseeker089

    truthseeker089

    Joined:
    May 6, 2019
    Posts:
    61
    list seems to be quite advance for a newbie like me lol I still haven't grasped it completely, how to use it, how to code it for my aim which is to destroy some objects of the same tag without destroying them all. thanks by the way!
     
  4. ikazrima

    ikazrima

    Joined:
    Feb 11, 2014
    Posts:
    320
    Arrays will also do. Btw this is better suited in the scripting forum.
     
    angrypenguin likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you have some criteria in mind for selecting which to destroy, or you just want to destroy some randomly?

    The below method DestroyAllWhoGetTails() creates an array of all GameObjects with the tag "Whatever" then flips a coin for each one, and destroys any who get tails.
    Code (csharp):
    1. public enum CoinFlip { Heads, Tails };
    2.  
    3. //Returns either Heads or Tails
    4. public CoinFlip FlipACoin()
    5. {
    6.     CoinFlip coin = CoinFlip.Tails;
    7.     if (Random.Range(0, 2) == 0)
    8.     {
    9.         coin = CoinFlip.Heads;
    10.     }
    11.     return coin;
    12. }
    13.  
    14. public void DestroyAllWhoGetTails()
    15. {
    16.     GameObject[] whateverArray = GameObject.FindGameObjectsWithTag("Whatever");
    17.     foreach (GameObject whateverObject in whateverArray)
    18.     {
    19.         if (FlipACoin() == CoinFlip.Tails)
    20.         {
    21.             Destroy(whateverObject);
    22.         }
    23.     }
    24. }
     
    SparrowGS likes this.
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,139
    I'm about to head out, but I actually had a lot of trouble with lists starting out as well. Thankfully, the Unity tutorial on them is easy to understand, follow, and expand on. In fact, it's part of their new intermediate scripting course now, which is really good.

    https://learn.unity.com/tutorial/lists-and-dictionaries?projectId=5c88f2c1edbc2a001f873ea5#

    If that's still out of your current skillset, the beginner scripting course is, from what I've looked at about it, also quite good.

    https://learn.unity.com/course/beginner-scripting
     
    angrypenguin and Ryiah like this.