Search Unity

Something Seriously Wrong With The Destroy Function

Discussion in 'Scripting' started by SirHoneyBadgerYT, Apr 15, 2019.

  1. SirHoneyBadgerYT

    SirHoneyBadgerYT

    Joined:
    Nov 1, 2018
    Posts:
    8
    So the Destroy() function will destroy a gameobject on the scene. Simple isn't it.

    You have a list of gameobjects and you write a function to destroy half of them.

    Guess what the it actually does, it destroys a random number of the gameobjects in the list. Not half, a random amount. Even though you specify a specific amount for it to destroy, it destroys how many objects it wants.

    Good one. Something is seriously messed up with this function, like it doesn't make sense.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Please show script you wrote, and use code tag.
     
    mdreptile and SparrowGS like this.
  3. SirHoneyBadgerYT

    SirHoneyBadgerYT

    Joined:
    Nov 1, 2018
    Posts:
    8
    Code (CSharp):
    1. for (int i = populationSize / 2; i < populationSize; i++)
    2. {
    3.     Destroy(rockets[i]);
    4. }
    I legit have another project where this works with no issue.

    But for some reason this function will not work at all, it'll remove no objects one time and then some random number the next.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    I would say it is an issue with your populationSize int. Add Debug.Log("Pop size " + populationSize); just before the for loop.
     
    xVergilx, dadude123 and Antypodish like this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Then it ain't the problem now, ain't it?
    what else is related to this?
    are you sure that all the objects you think are in the list are actually in the list?
     
    Joe-Censored and Antypodish like this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    As guys suggest, check if objects are in the list as expected.
    Usually people thinking they found a bug, it results that they had actually problem with logic in a code.
    So double and triple check your list.

    Debug.Log is your friend.
     
  7. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    If a fundamental method behaves wildly unpredictably... for example:

    1) Destroy
    2) floats
    3) GetComponent
    4) any number of methods that every user of Unity would have used within the first week of tinkering with Unity

    I would:

    a) google if people have seen this similar behaviour (such as if you google "unity floats weird" you'll get tons of results about float acting weird and explanations why)

    b) if a resulted in nothing, it's safe to assume it's more likely your code and not the fundamental function/type in Unity. Review your code with a debugger, see if you can locate the issue.

    c) if you still can't figure it out, ask here and supply your code because it's still likely your code causing it and you just aren't seeing what it is

    We can assume (b) if only because Unity is over 10 years old. And if these methods acted wildly unpredictably... you would have found articles/posts/results about it when you googled for it. Otherwise, how are those of us who have published games able to release functioning code if these fundamental functions/types don't behave in a predictable manner?

    With that said... there are bugs that exist in all systems. And you may have tripped over something that is truly an issue. But the likelihood of that is low when it's things like "Destroy" or "float" or many of the other topics that crop up on the forums. It's usually edge cases like "When I set the mecanim animator to this specific state I get an odd behaviour with this specific animation".

    ...

    And honestly this goes for really any programming you do.

    I write .net in the business world as my day job. And in the forums and exchanges I hang out in for that we see the same repeating questions about possible "bugs" for fundamental stuff in .net. Considering the number of people who write this stuff every day, what are the odds you tripped over the glitch in the system standing out in the open?

    This is the equivalent of finding a wormhole to an alternate dimension in the middle of time square. Sure, it could be a thing... but with the number of people walking by it every day, what are the odds you're the first to see it?

    The reasonable assumption is to assume you're seeing things before assuming it's real. That is until others go "yeah, I see it too!!!"
     
    Last edited: Apr 15, 2019
    Kurt-Dekker, Ryiah, xVergilx and 2 others like this.
  8. SirHoneyBadgerYT

    SirHoneyBadgerYT

    Joined:
    Nov 1, 2018
    Posts:
    8
    populationSize is a fixed variable, it doesn't change
     
  9. SirHoneyBadgerYT

    SirHoneyBadgerYT

    Joined:
    Nov 1, 2018
    Posts:
    8
    fam what are you on
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    I suggest look into points, highlighted by @lordofduct.
    For example, 35 / 2, is 17.5. As floating point and rounded, that can be either 18 or 17,due to floating point error.
     
    Last edited: Apr 19, 2019
    xVergilx likes this.
  11. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    fam, lol

    good luck bruh
     
    Stardog, SparrowGS, xVergilx and 2 others like this.
  12. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Also, try outputting an actual list.Count. Maybe its count is not what you're expecting.
     
    Antypodish likes this.
  13. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Im not from an english speaking country and I just googled "fam", it made me sad for the english language.
     
    Ryiah, xVergilx and Antypodish like this.
  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    lol

    mind you, it's one of those words that most people cringe at hearing in the States. It's one of those slang words that caught a fad, but then quickly got derided because it became that slang word "your parents use". And is now usually used ironically (hence SirHoneyBadgerYT calling me fam, he was making fun of me by ironically using the word against me. Followed by my use of 'bruh', ironically jabbing back at him).

    I bet there's still sub-cultures who still use it unironically, but this certainly isn't one of them.
     
    xVergilx and SparrowGS like this.
  15. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    PoppulationSize / 2 change this to mathf.FloorToInt((float)poppulationSize, 2.0f)
    Its not uncommon to have int float divide issues that result in unwanted behaviour.
     
    Antypodish likes this.
  16. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    For me initially, without knowing exact meaning of used phrase, it sounded to me a bit disregard, as it was. But I didn't want go down the rabbit hole, so I skipped to comment. I don't expect however, hearing such phrases from someone, who is looking for answers and assist, to be honest.
     
  17. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    yeah, that part was kind obvious, i could hear in the comments "tone".
     
  18. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Your for loop doesn't make much sense to me if populationSize is a constant. Your rockets list changes length. Let's say your populationSize value is 36. Your code will only destroy rockets at indices 18-36. If you have less than 36 rockets, you'll get a null reference exception. If you have more, anything after that won't get destroyed as your for loop stops at 36. In order for your code to work, you need to set populationSize equal to the rockets length just before the for loop runs.
     
  19. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you want it to destroy half the list then you should be looping based on List.Count instead of populationSize. If List.Count and populationSize are different values you will get more or less than half the list destroyed.

    This will destroy exactly half of the list for example.
    Code (CSharp):
    1. int index = 0;
    2. int nextToDestroy = 0;
    3. if (rockets.Count > 0)
    4. {
    5.     while (index < rockets.Count)
    6.     {
    7.         if (index == nextToDestroy)
    8.         {
    9.             Destroy(rockets[index]);
    10.             nextToDestroy += 2;
    11.         }
    12.         index++;
    13.     }
    14. }
     
  20. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    @Joe-Censored, I'm not sure that method actually would destroy half of the rockets, as each time you destroy a rocket, count decreases in value. Unless I'm mistaken (since I haven't tested your code to verify), you'd need to set a variable, like populationSize = List.Count and loop over that instead.

    Alternatively, a foreach loop could work with a simple bool flag to destroyNext.
     
  21. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    I tend to dislike while loops unless actually needed, i'd write such a function like this maybe.
    Code (CSharp):
    1. for(int i = 0; i < rockets.Count){
    2.    Destroy(rockets[i]);
    3.    i += 2;
    4. }
    or even (although it achieves the same with double the iterations)
    Code (CSharp):
    1. for(int i = 0; i < rockets.Count){
    2.    if( i % 2 == 0){
    3.       Destroy(rockets[i]);
    4. }
    5. }
     
    Joe-Censored likes this.
  22. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    That's still pretty much a while loop. The point of a for-loop is being able to put all the driving code on top. Use: for(int i=0;i&lt;count;i+=2).
     
  23. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Calling Destroy doesn't actually destroy the object immediately. It schedules the destroy, which will then happen after the current frame has completed.
     
    RockyWallbanger likes this.
  24. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Apologies then, I wondered if that might be the case
     
  25. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,998
    No, and JoeC's reply also misses this: Destroy(A[3]) changes A[3] to null. Destroy can never change a list count. You'd have to use Remove.

    The OP's code appears to null out the _end_ of the list, then shrink the pop count to cover it (a standard trick). But then HoneyBad writes that pop size never changes, so it's just a muddle.
     
    RockyWallbanger likes this.
  26. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Another point for OP to keep in mind is if he just dragged a bunch of GameObjects into that list at once, they are not necessarily in any particular order. That could make it seem a bit random.