Hi, I have a 2D array of GameObjects. I want to iterate through this list and copy some of the gameobjects that meet a specific criteria to a separate 1D array. Once I’ve done this, I want to iterate through this second list and use it to delete the relevant objects from the first 2D array (so the single dimension array is a subset of the 2D array).
I’m sure there are better ways to do this - I know I could just flag the objects in the first list, but bare with me Do I need to store a list of pointers to the objects in the 2D array? If so, could someone give me an example, bit rusty in that area…
Is the second list purely for storing the items for deletion? You don’t want to use that list for anything else? If so, you could just store the index in the second array - i.e. just an array of ints.
You’re right though - whatever it is that you’re trying to do, it sounds like maybe you’re going about it in a backward kinda way
Ok, I’m trying to create a simple match3 game. I have a 2D array of gameObjects. I also have a collection of functions in which one of these objects is passed, and I scan in all four directions to see if there’s a match of adjeactent gameobject.
WIthin each function, if there is a match with the gameobject that was passed, I add it to a list. Then in a separate piece of code I check this list. If there are 3 objects in the list, then I want to destroy them from the 2D array. If there are less than 3 (ie not a match3) then I set the list to null
Well merely removing the game objects from the list doesn’t seem to solve your issue though, since gravity should put new objects in their place, right?
So although this destroys the object, it doesn’t seem destroy it from the 2D array as well (it still shows up in debugger), which is why I’m confused - I thought they were the same object. So my question is, to delete a gameObject out of a 2D array, do I have to delete the object by calling it’s destroy method, or set it’s reference to null in the 2D array, or both? I thought they were the same object!?
Usually, as long as you keep at least one reference to an object, it will stick around. I’m not sure what Destroy does, but after it’s done the gameObject reference is most likely useless. You’ll have to set the reference in the array to some other value, like null, before the remnant can be removed from memory by the garbage collector.
So yes, they are the same object, and yes, you’ll have to both Destroy and set to null.
Whether that’s the best approach is another thing. If reuse is an option, you could disable the object and put it back in its pool instead of destroying it.
Thanks, tried it and indeed it does. Should have done so first. Calling Destroy will make the corresponding array entry null. So if nothing turns up null, whatever you destroyed wasn’t part of the array. What’s the debugger showing?