Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Loles · Sep 13, 2020 at 11:12 AM · prefabinstantiate prefabprefab changing at runtime

Change hierarchy of prefab in script

Hello!

I am trying to create "a prefab" in script, so I can Instantiate it freely whenever I want. To be specific, I have a Unity prefab "Player" -> that should be a tank with two parts - body and gun. So I have also prefabs of many Bodies and many Guns so player can choose from those. Now I want to create something like playerPrefab object in script, whose instantiated gameobject will contain all those parts.

For now it looks like this:

     GameObject playerPrefab = ...
     GameObject playerPrefabBody = ...
     GameObject playerPrefabGun = ...
     
     playerPrefabBody.transform.parent = playerPrefab.transform;
     playerPrefabGun.transform.parent = playerPrefab.transform;
     
     .....
     
     GameObject player = Instantiate (playerPrefab, ...);

I'm still getting this:

 Setting the parent of a transform which resides in a Prefab Asset is disabled to prevent data corruption (GameObject: 'Body 2').
 UnityEngine.Transform:set_parent(Transform)

I was googling a bit, but hadn't found anything useful. I know, I can instantiate all three prefabs and THEN join them, but that would be ugly, as i will need to do that every time player is beeing spawned (which could be once each 5 seconds, or even less. Well, that sould not pose any problem, but it's still ugly :D )

Just to point out, I don't really want to change the prefab itself, as it will be used for the same player, when he chooses other type of Body or Gun...

Is there any way to do this as described?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Sep 13, 2020 at 11:37 AM

@Loles, we do this all the time. All you need to do is add the instances to the parent instance. The general syntax is instance transform.parent = parent instance transform. You must create instances of all the objects involved first.

For example:

  GameObject playerPrefab = ...
  GameObject playerPrefabBody = ...
  GameObject playerPrefabGun = ...

  GameObject player = (GameObject) Instantiate( playerPrefab, ... );
  GameObject body = (GameObject) Instantiate( playerPrefabBody, ... );
  GameObject gun = (GameObject) Instantiate( playerPrefabGun, ... );

  body.transform.parent = player.transform;
  gun.transform.parent = player.transform;

  // or if you want the gun on the body and the body on the player
  gun.transform.parent = body.transform;
  
  


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

Answer by Loles · Sep 13, 2020 at 11:59 AM

@streeetwalker Yes, I know, I can do that. I was wondering, if i can somehow prepare that prefab in script and then do only ONE call of Instantiate function, as I will be calling it in more than one places (spawning on login, spawning on next map, spawning upon death), and also to all players logged in the game (as it will be an online game) (many players should Instantiate on one Client with that function).

Comment
Add comment · Show 3 · 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 streeetwalker · Sep 13, 2020 at 12:15 PM 0
Share

@Loles Sorry, I misunderstood - I thought you wrote you did not want to alter the prefab. The short answer is, no, you can't alter prefabs through script while a game is running. You can do it in an editor script.

avatar image streeetwalker · Sep 13, 2020 at 12:17 PM 0
Share

@Loles, on futher thought, you can, for example the code I pasted, create the composite once up front, and then just make instances of the composite game object.

avatar image Loles streeetwalker · Sep 13, 2020 at 07:06 PM 0
Share

@streeetwalker You've said "create the composite once up front". That looks really promising. Can you paste here an example of that?

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

248 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to spawn a prefab at a duplicate objects location 1 Answer

How To Reset Prefab Values after exiting Playmode? 0 Answers

How to Change the Variables of an Instantiated Object? 0 Answers

Unity overwrites prefab values in runtime 0 Answers

Instantiate works fine in Editor and does not work after building game 0 Answers


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