Timeline for Properly destroying game objects from a List that is traversed in FixedUpdate()
Current License: CC BY-SA 4.0
8 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 15, 2024 at 22:23 | comment | added | Gerry Schmitz | When a cat is "killed", I'd leave it dead ... then in the Update "for" loop, check for dead cats. If dead, add that cat to a local "dead collection". Once you exit the for loop, delete any cats in the "dead collection" from the main "cats" collection. Avoids this particular "race" condition. In the rest of the app, simply test for "if cat not dead" when you encounter a cat reference. This is inline with the pattern of leaving the dead "laying around" for a time before "disappearing" them. | |
| Nov 9, 2024 at 18:00 | comment | added | DMGregory♦ | Can you provide a Minimal Complete Verifiable Example? Once we understand how these scripts inter-relate, we can find a solution to help them play nicely together. But that's more challenging if we can only see half the situation — we might come up with an over-engineered solution that handles every possible case when a simpler one would cover your use case. | |
| Nov 9, 2024 at 16:59 | comment | added | tariq |
I'm guessing they are, but the question is how to prevent that. The CatShouldDie method can be called at any time from another script.
|
|
| Nov 9, 2024 at 16:08 | comment | added | DMGregory♦ | Are they getting removed in the middle of the iteration? | |
| Nov 9, 2024 at 15:26 | comment | added | tariq | No, it's just a single scene and they don't get destroyed anywhere else. | |
| Nov 9, 2024 at 11:44 | comment | added | DMGregory♦ | Do you destroy the cats from anywhere else? The code you've shown removes the cat from the list, so if you only ever get the error on cats that are still in the list, they must have been destroyed some other way, to still be in the list. Is this list persistent across scene loads, perhaps? | |
| S Nov 9, 2024 at 1:56 | review | First questions | |||
| Nov 9, 2024 at 5:30 | |||||
| S Nov 9, 2024 at 1:56 | history | asked | tariq | CC BY-SA 4.0 |