- Home /
Replace GameObject with another GameObject and preserve references without using Destroy()
This question has been asked before but nobody has ever actually answered it. Everyone has just provided work-arounds and alternate solutions.
In my case, I am writing a plugin that needs to replace a gameobject with another. The specific gameobject I am replacing is UNKNOWN to me. And the potential references that may exist in my client's project to this gameobject is UNKNOWN to me.
IT IS THEREFORE IMPOSSIBLE for me to create some cheap hack solution that involves destroying the old game object, and spawning a new one in its place, and then attempting to "update the references" everywhere in their project, since I know nothing about their project, or the references, or even the gameobject.
I need to ACTUALLY REPLACE the gameobject in a way where UNITY will NOT break the references, because UNITY DOES NOT consider the gameobject to have been destroyed. NO CALLS to Destroy(). An actual bona fide replace is needed. This ought to be possible, surely I'm not the first person who has ever needed this.
(sorry for the forum-like post, but it's not working enough to be an answer; I've just seen formatting is terrible in comments, so maybe we can find a good thread on the Unity forums or start one to discuss this ins$$anonymous$$d)
Same need here. Since game objects in Unity are purely ECS-based with no class deriving from GameObject (unlike e.g. Unreal Engine with derived Actor classes), I considered stripping all the components of the replaced root game object out, replacing them with all the components on the replacing root game object (with the exception of Transform, for which you would copy-paste the component values). $$anonymous$$aybe that would fit your need?
In my case it wasn't enough, as: 1. $$anonymous$$y replacing game object is a prefab, and I want to have a prefab connection to the replacing prefab, even if it has no relationship with the replaced gameobject (in my case, at least the replacing prefab was a Variant of the replaced prefab instance) 2. Both have children with almost the same hierarchy, and I want to preserve references to the children too
And some developers may also need to: 3. Preserve references to non-Transform components on the root object
If you want to preserve children but consider them as brand new objects and don't care keeping their IDs (unlike the root), then you don't need 2. You can just copy the children of the replacing game object under the replaced gameobject (you don't even need a recursion here; copying the 1st-level children will also copy the grandchildren).
I'm trying to solve each of these issues: 1. I searched an API to create an artificial connection to an arbitrary prefab, or even a related prefab, but couldn't find a method like thhat in PrefabUtility 2. We need to define how we identify matching children... $$anonymous$$atching by name in hierarchy (like the animation system) seems a good start, providing you don't change the name of your children in your prefab instances (unfortunately I sometimes do, to make them easier to search in the Scene reference popup) 3. We need to check each and every component and see if they match one in the prefab... So we can only paste components by values ins$$anonymous$$d of replacing them entirely.
If I could manage to preserve/create the prefab connection, it may solve most issues: I would be able to preserve all components in common, and simply apply a Revert to apply remaining changes and become like the replacing prefab.
Your answer
Follow this Question
Related Questions
Is a unity object really destroyed if its destructor was never called? 1 Answer
How can I replace an object while keeping the references to that object? 2 Answers
Object reference not set to an instance of an object. 2 Answers
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer
Destroying Gate by hitting a switch 1 Answer