• 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 jessee03 · Oct 22, 2011 at 04:28 AM · swap

swapping gameobjects

I've tried a bunch of different combinations of code and searched but can't find a simple answer on how to swap one gameObject for another. I just wanna change out one mesh for another mesh.

 var gameObject1 : GameObject;
 var gameObject2 : GameObject;
 
 gameObject1 = gameObject2;


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 Fattie · Oct 22, 2011 at 09:47 AM 0
Share

It's very likely you don't want to swap the mesh. You actually wanna swap the whole damn thing !!! Sycla has already exactly explained how this is done. Swapping one prefab for another is a very common part of Unity games, it happens constantly in gameplay. In case it helps anyone in the future, I pasted in the relevant manual section below and some other explanation.

1 Reply

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

Answer by syclamoth · Oct 22, 2011 at 04:38 AM

var mesh1 : Mesh; var mesh2 : Mesh;

GetComponent(MeshFilter).mesh = mesh2;

Like that!

For swapping out a mesh with a destroyed variant, do something more like this-

// Make a prefab with all the correct behaviour on it, and drag it here- var destroyedPrefab : GameObject;

// Then, when the time comes- Instantiate(destroyedPrefab, transform.position, transform.rotation); Destroy(gameObject);

This will destroy your gameObject, and replace it with a new one from a prefab. Sorry, I thought you wanted an appearance-only change! This is more destructive, but much easier to manage.

Comment
Add comment · Show 6 · 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 jessee03 · Oct 22, 2011 at 04:53 AM 0
Share

not able to do this with just gameobjects ? o,o

avatar image jessee03 · Oct 22, 2011 at 05:24 AM 0
Share

Okay I was able to swap the mesh but the only problem I have now is it's animated and breaks up into pieces and my main mesh won't show up in the object list but all the little pieces do show up.

avatar image syclamoth · Oct 22, 2011 at 06:16 AM 0
Share

Ok, that's a much more complex problem. How exactly is it animated? Are both of your meshes animated in the same way? For this kind of job, I would keep a 'base' (invisible) skeleton, and then make the actual models not animated, and make each individual bone a child of the corresponding bone on the base skeleton.

Then, to swap animated meshes, delete the existing visible model, and then apply the same process of splitting up the hierarchy and then reparenting to the base skeleton to the new one.

(Please note that this requires keeping a list of the bone's original parents, and keeping track of every bone involved so that you can cleanly remove them. Because you are splitting the original hierarchies, you won't be able to use shortcuts like relying on the fact that GameObject.Destroy removes all the transform children as well).

avatar image jessee03 · Oct 22, 2011 at 06:24 AM 0
Share

How it works right now is a simple unity box mesh is spawned and then once the player collides with it it's swapped with an imported animated object which is a box that breaks apart into a bunch of different pieces(done with the fracture tool in blender). So what happens is the main object is swapped with the new object that has a fracture animation ( that's why this game object has a bunch of different pieces attached to it. unity recognizes all the pieces as a mesh object except the main part that holds all the pieces and animation information.

That's why I wanted to swap gameobjects so it would be easy to play the animation the second object has.

avatar image syclamoth · Oct 22, 2011 at 06:30 AM 0
Share

Oooh! Well, that's also much more straightforward. Let me change my code...

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Swap Custom Cursor for Animated Movie Texture or Sprite Sheet? 1 Answer

How can i swap a models mesh using script 1 Answer

Substituting between two gameobjects transform position 1 Answer

Swapping Skeleton / Adding bones to a hierarchy and sharing animation 2 Answers

COuld someone change this line of code to java script please if it is possible? 2 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