C Sharp and pointers to gameObjects

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 :slight_smile: 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…

Thanks in advance

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 :slight_smile:

Yeah it’s precisely that - a list of items to delete from the initial 2D array.

It’s hard to tell whether your concept is a good solution for your problem as your problem is not clearly defined.

Could you provide a more abstract description of what you want to solve and some more information on this 2D array?

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?

I create the array like this:

	private void PopulateMap ()
	{
		GameObject tempGameObject;
		
		for (int z = 0; z < numberOfRows; z++) {
			for (int x = 0; x < numberOfColumns; x++) {
				tempGameObject = ReturnRandomObject ();
			 
				
				
				 Map[x, z] = Instantiate (tempGameObject, new Vector3 (referenceToAnchorTransform.position.x + (x * gapBetweenTiles), 20.0f, referenceToAnchorTransform.position.z + (-z * gapBetweenTiles)), Quaternion.Euler (new Vector3 (0.0f, 180.0f, 0.0f))) as GameObject;
				
			}
		}

And when I find an object I wnat to destroy, I’m accessing a method in the gameObject that destroys itself:

	public void DestroyObject ()
	{
		Vector3 tempPosition = transform.position;
		
		Destroy ( gameObject );
		Instantiate (explosion, tempPosition, Quaternion.Euler ( new Vector3(0.0f, 180.0f, 0.0f) ) );
		 
	}

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!?

…so if the reason this is happening the fact that the 2D array is just a list of pointers to the gameObjects?

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.

Any Unity objects removed via Destroy will have all references to them evaluate to null.

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?