- Home /
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!
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.
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.
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.
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);
}
}
}
No problem, it's very simple but if youre new to unity, it's quite a challenge. Good luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220613182103im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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