Destroy all overlaying GameObjetc in an Array C#

Hi i’m Trying to errase all GameObject that i put in an Array.
for this i use this code

bool createArray = true;
private GameObject[] objectArray;
void Update()
{
  if (createArray == true)
 {
   // Here i create my Array
   objectArray = new GameObject[4, 4, 4];

   // here i put two objects in the same square
   objectArray[1, 1, 1] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity));
   objectArray[1, 1, 1] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity));

   createArray = false;
 }
}

// Create my destroy button
OnGUI()
{
  if(GUI.Button(new Rect(x, y, Width, Length), "Destroy")
  {
    // Here i ask to destroy each object in the Array But he wont destroy the 2nd object that is in the same square (sadly)
    foreach (GameObject objet in objectArray)
                        {
                            Destroy(objet);
                        }
  }
}

So the problem is that if i put 2 GameObject in the same square of my array i will erase the first one but not the 2nd one.
How can i do to destroy every things ?

Do you know what a pointer is ?
You have only one object in that array .
The second object .
If you want to have 2 objects :
objectArray[1, 1, 1] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity));
objectArray[1, 1, 2] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity));

I’m not trying to add two objects in my array (thanx for me i dont need help for this :wink: ).
It came that 2 Game object are instanciated in the same square like above. I 'm trying to destroy both gameobject (the code actually just remove the first one).
I 'm newbee programmer and don’t know so much about pointer (just a quick look on) only that it is some directive that point to the adress but in C# i think it’s totally transparent.
And then if you try the code above you will see that the two GameObjects are instanciated in the scene.I can put 2 gameObject in the same array square.

You didn’t get the point …
What do you mean by “array square”?
An array is like a list of address for those objects that he contains .
When you initialize 2 objects with the same address the last one will get the address . So when you execute this line of code .

foreach (GameObject objet in objectArray) 
                        { 
                            Destroy(objet); 
                        }

the compiler will find only one valid address.
Try replacing the code I put in my first replay and you will see that both objects will be destroyed

Yep i know that this is working like that but i’m in a case were many GamesObject are instanciate at the same adress in my array (what i call array square)as described in the code. Is there a way to destroy both objects, that is the question.
I Haven’t only 2 objects instanciated at the same adress this was just for the exemple. actualy i have like hundred instanciated gameObject in the array and some are at the same adress.

I think what cosmin25 is trying to say is that you have a bug in your code.

you have these two lines

// here i put two objects in the same square 
objectArray[1, 1, 1] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity)); 
objectArray[1, 1, 1] = (GameObject)Instanciate(myprefab, new Vector3(x, y, z), Quaternion.identity));

You’re creating two objects, just like you want, but you’re storing their address at the same index in the array. The end result is you losing the ability to destroy the first object, since it was overriden by the second object. If you change it to his suggestion, it’ll put the second object into the second index in the array, therefore allowing you to iterate the array and destroy them properly. Copy+Paste can be a programmers savior, but it can also be their executioner.

OMG thanks both of you i just get my answer with your explanation!
I think there is two way to solve my problem.
the first one is to follow your advise and then create a 4 dimentionnal array using the 4rth dim as an index of my object layer.
2nd option is to create an array in my array but knowing me i’m gonna be confuzed too fast.
Anyway thanks both for the help.
:wink:

ps: I understand the adress trick the object is instanciate but the 2nd instanciation overwrite his adress so it is always existing but i can’t acces it anymore so i can’t delete the gameObject.