0
\$\begingroup\$

For example, I would like to change a cube into a sphere, then change that sphere into a capsule, by pressing the up arrow. I would also like to be able to change in the other direction. Change the capsule into a sphere, then change the sphere into a cube, by pressing the down arrow. It would be great if i could add extra objects in, say a pyramid etc.

Ultimately i would want to do this with something like a seed, sapling, and tree. Each with there own properties.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ What have you tried and what specific problem did you get stuck on? This "change", do you want to animate something or just change a model by button press? \$\endgroup\$ – Peethor May 15 '19 at 8:07
1
\$\begingroup\$

You can do it using Prefabs and array of GameObjects.

For instance:

  1. Declare an array of GameObjects

    [SerializeField] GameObject[] MyObjects;

  2. "Draw" and put each objects into that array (drag and drop using UnityEditor)

  3. Attach a script to an empty gameobject (say: Player) and use Update function to change object

    void Update() { if (Input.GetButtonDown("Fire1")) { Instantiate(MyObjects[index], Player.transform.position, new Quaternion(0,0,0,0); index = index +1; if(index > MyObjects.Lenght) index = 0; } }

Look at :

  1. https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
  2. https://unity3d.com/fr/learn/tutorials/topics/scripting/arrays
\$\endgroup\$
2
  • 1
    \$\begingroup\$ If you want to have it cycle properly through this array you'll also need a method that increments or de-increments a value you can use as the index value of your array. Don't forget to destroy the previously instantiated objects too, else you'll end up with lots of GameObjects spawned in the same place. \$\endgroup\$ – AnarchyFox May 15 '19 at 9:57
  • 1
    \$\begingroup\$ Thanks so much for the help. \$\endgroup\$ – McStag May 15 '19 at 10:20
0
\$\begingroup\$

If you have several behaviours you want to stay on the object and retain their data after the "metamorphosis", then it might be a good idea to have one game object without a renderer which carries all the shared behaviours and add child objects which represent the different appearances of the object. Deactivate all except the one you want to show at the moment.

But keep in mind that this solution is not very memory efficient. So if you have a very large number of such objects in the game at once (thousands) it might be suboptimal. Destroying and instacing might be better in that case. But if that happens a lot, memory fragmentation might become an issue. Object pools would be a solution, but that would again cost memory. You see, this rabbit hole goes deeper and deeper.

A compromise between both methods could be to retain the object and only exchange the Mesh of its MeshFilter / Sprite of its SpriteRenderer

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.