Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Calling a function via button from instance of prefab 1 Answer

How to make my camera follow a prefab and update it to follow the instance of same prefab the generated after the same prefab ? 0 Answers

Unity 2D Game Issue: Script is only affecting one prefab instead of all of them. 0 Answers

Instatiate to the next scene prefabs from an array 1 Answer

Prefab clones aren't deleting? 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