• 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
0
Question by jacky-kschou · Jun 08, 2015 at 10:48 AM · scripting problemprefabs

Truly Truly (I really mean it) replacing a non-prefab GameObject

Hi,

I am working on a nested prefab system.

I want to replace a GameObject (which is not a prefab, I cannot make it a prefab because it is already a child of a big prefab) with a new one, not only just setting the data of transform to be the same as the old one, but also all the Monobehavior that refers to the GameObject are replaced as well (so after destroying the old GameObject will not result in a Missing GameObject error),

Is there actually a way to accomplish this?

Thanks

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Fattie · Dec 29, 2015 at 03:39 PM

  1. Any time you want, you can just (a) get rid of a game object (say, some character) and then (b) instantiate a new game object there (say, a new version of that character). Let's refer to the new object as newRobot.

  2. Be careful that you will probably want to set the parent properly in these cases.

  3. I think you are asking: you have a number of other scripts which refer to oldRobot - what then happens? Unfortunately there is no possible magic solution here, quite simply you have to change those references to newRobot. How else could it be?

But note - of course, some script has to destroy the oldRobot and make the newRobot. Obviously, that script then "knows about" the newRobot !! So it's a non-issue.

It could be the problem is you have some other scripts which also refer directly to the oldRobot.

If so, that is basically your problem - you shouldn't do that.

Let's say that robotBoss is the script instance which controls oldRobot and later newRobot. So, inside robotBoss you have a variable robotBoss.theRobot

Quite simply, all your other scripts which for some reason need to refer to the (old or new) robot, should only be referring to robotBoss.theRobot ...

... they should not be directly connected to the robot.

Fortunately, the solution is that simple. Its' a general and very helpful software principle that you have a "single point" for anything (whether controlling something, accessing something, or whatever the case may be).

(Indeed more likely those other scripts would only use functions inside of robotBoss which asks robotBoss to do stuff in relation to the robot .. so, robotBoss.MakeRobotRun() or robotBoss.RobotSoundEffects() or whatever ... the other scripts would not directly talk to the robot.)

Comment
Add comment · Show 1 · Share
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 Fattie · Dec 29, 2015 at 04:16 PM 0
Share

glad to have helped!

it's very likely you're looking for the "Grid" system in Unity ...

Here's a recent version of the "Grid" system ...

http://answers.unity3d.com/questions/663351/design-advice-power-up-system-static-variables.html

one of many questions discussing it ..

http://answers.unity3d.com/questions/551297/how-can-i-instantiate-a-prefab-from-a-static-funct.html

hope it helps!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Serialize script fields of prefabs with other prefabs, so that when I drag them into hierarchy, the connection becomes between game objects? 1 Answer

How can i add all the prefabs in the assets directory and sub directories to List or Array ? 0 Answers

Problem During Instantiation 1 Answer

Script inside instantiated prefab run lag behind 1 Answer

How to access the particular instance of the Prefab ? 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