Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by $$anonymous$$ · Sep 14, 2016 at 08:41 AM · objectdestroyreferencereplace

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image huulong · Apr 15, 2019 at 06:36 PM 0
Share

(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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges