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 kfchristian15 · Jan 31, 2015 at 08:28 PM · c#scriptingbasicschangemeshcollidermeshrenderer

How to change shape of gameObject after start

I want to make it so that the player starts off as a cube, but if you hit the "f" key, the player will change into a sphere. I'm fairly new to unity and I was wondering what the best way of doing this was. Would it be to instantiate a sphere with the same scripts on it as the cube and destroy the cube, and do the opposite when "f" is hit again, or can you change the object's shape itself (as well as the mesh collider)? This is the idea script I have now, though I know that "sphere" and "cube" are not the ways to change the object, I'm using them as placeholder's currently. Thanks!

Comment
Add comment · Show 3
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 PhoenixBlackReal · Jan 31, 2015 at 09:03 PM 0
Share

Changing the mesh itself would requite to create new vertices and triangles - really too much of complexity if you're just starting. I would suggest having both the cube and sphere and make it so that the f key enables or disables either of them - in such way you wouldn't have to use Destroy() and instantiate which might be a bit too slow and visible.

avatar image LukaKotar · Jan 31, 2015 at 09:36 PM 0
Share

Don't instantiate objects if you don't have to. Parent the sphere and the cube to the same object (so they share the same position), put your scripts on that new parent objects, and just disable the cube and enable the sphere through code.

avatar image sam2k13 · Feb 01, 2015 at 02:14 AM 0
Share

Try using scripting to change the mesh in the mesh renderer component. This way, if you need to attatch a script to the cube or triangle object they will be the same ga me object.

1 Reply

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

Answer by FezAstron · Feb 01, 2015 at 10:09 AM

I think the best way is to create 2 gameobject variables, I use Javascript but I'll try my best in both. Create an empty gameobject in the editor and put in a sphere and cube in it with the position X:0 Y:0 Z:0. Then drag the cube and sphere into the script Gameobject, I'm sure you know that. This way, if the cube weighs 100 and the sphere weighs 50, they will keep their properties or if you added specific scripts to them, they keep it and stay unique.

C#:

 GameObject sphere;
 GameObject cube;
 
 void  Update (){
   if (Input.GetKeyDown(KeyCode.F)){
     if(sphere.active == true){
       cube.transform.position = sphere.transform.position;
       sphere.SetActive(false);
       cube.SetActive(true);
     }else if(cube.active == true){
       sphere.transform.position = cube.transform.position;
       cube.SetActive(false);
       sphere.SetActive(true);
     }
   }
 }

Javascript:

 var sphere : GameObject;
 var cube : GameObject;
 
 function Update(){
   if (Input.GetKeyDown(KeyCode.F)){
     if(sphere.active == true){
       cube.transform.position = sphere.transform.position;
       sphere.SetActive(false);
       cube.SetActive(true);
     }else if(cube.active == true){{
       sphere.transform.position = cube.transform.position;
       cube.SetActive(false);
       sphere.SetActive(true);
     }
   }
 }

Comment
Add comment · Show 2 · 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 kfchristian15 · Feb 02, 2015 at 08:18 PM 0
Share

This is great, thanks!

avatar image FezAstron · Feb 02, 2015 at 08:20 PM 0
Share

No problem, it's very simple but if youre new to unity, it's quite a challenge. Good luck!

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Change output rotation of this Lookat script? 2 Answers

C#, "object reference not set to an instance of an object" 3 Answers

How to active mesh renderer with a script C# 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