Destroying child gameObject also destroys that child on all duplicates.

Hi everyone.

I have a room where the walls are broken up into pieces so that certain parts can be removed as needed. Each piece of wall is parented to it’s own empty GameObject, which in turn is parented to the room.
The empty GameObject has a script that, among other things, determines if and when the wall it is the parent of should be removed. The empty GameObject has a name such as “L1-2wide”, and the child would be called “L1-2wide_wall”.
I have more than one room and the script locates another wall nearby and gets a reference to it.

To delete the wall I have this in my script:

for(var child:Transform in this.transform)
{
    if(child.name == (this.gameObject.name + "_wall"))
    {
        Destroy(child.gameObject);
    }
}

This approach worked perfectly fine and I had no issues. In order to write a new piece of code, I changed the names from the example format above to include “Group1” or “Group2” before the rest of the name. This is the only change I made. Now, the same script destroys not only the appropriate wall, but the wall of the same name in the other room. No reference to that specific wall was gotten in the script.
I duplicated the walls and unparented them to see if it was deleting based on name, regardless of parent, but those were left untouched by the script.

I then made a Transform variable called wallObject in the script and on every instance of the script I dragged the appropriate child wall into it in the inspector and used this to destroy them as needed:

Destroy(wallObject.gameObject);

Once again, it destroys both the wall it should and the wall of the same name in the other room.
I’m pretty stumped as to why this has happened.
I removed the Group1 and Group2 parts from the names and it goes back to working fine.

Any insight on this thoroughly confusing matter is appreciated. =)

EDIT: After messing about with it some more, it isn’t actually destroying the other wall.
What seems to be happening is that the objects being deleted share a mesh filter when duplicated and when one is deleted, it removes the mesh filter from the other.
Is there a way to stop it sharing a mesh filter or prevent the mesh filter from being removed from one when the other is destroyed?

Sounds like you simply need to debug this better, there will be a reference problem somewhere you’re not seeing.

Log out instance ID’s instead of destroying. Ty EditorGUIUtility.PingObject and Debug.Break so you can step through each time it thinks it’s found a match and see what child is actually referencing. Debug.DrawLine can be handy for this too, just draw giant lines up from the base of the object.

When something like this is happening in your script, don’t change the script unless you’re 100% sure you know what the cause is. There’s no point taking a 2nd stab in the dark, you need to find the light switch so you can finally see and understand the problem and there are a lot of tools avaliable inside unity to assist with this.

  • if(child.name == (“_wall”))
  • {
  • Debug.Log("Destroying " + child.GetINstanceId();
  • Debug.Drawline(child.transform.position, child.transform.position + Vector3.up * 5f, Color.red);
  • Debug.Break();
  • //Destroy(child.gameObject);
  • }

Some of those are unfamiliar to me so I’m glad to have learned of them. They’ll surely come in handy in the future, thank you. =)

I had, however, already done something similar to this, printing to the console and drawing lines. These have been written into the code from the start, since I knew I’d likely have troubles, and everything seems to be working as intended.
After closing the project earlier, now that I’ve come back to it the problem no longer persists though nothing has changed. Given that it was removing the shared mesh in such a way, next time I’ll try reimporting the objects and see if that helps the issue.

Thanks for your time and advice! =)