Switching prefabs (253520)

im looking for a way to have unity at button press switch prefabs at the same position at button press as the game is ment to have the user only have x ability after a switch that has a unique sprite

what would be the easy’st way to do this i have unity playground loaded

this is in Unity 2022.1.4f1

Not fully sure of what you're asking? As a prefab is no different that just a plain old Object. If you are completely new to coding, I strongly suggest watching several youTube videos, that hopefully each new video teaches you something new, and eventually what it is you're looking for will eventually be answered. If you come up with some code, and it doesn't seem to be working right, then please by all means, paste the code in here, so we can take a look at it. And find that nasty bug and squash it!

–

with a key press change the playable character to a different prefab that has 1 extra thing it can do and a different sprite it should be the main character can only jump but the other prefab has a uncapped jump so it can "fly" around the level and has a separate model to indicate that the playable character is in "flight" mode

–

1 Answer

1

Ok… I’ll go out on a limb and assume you don’t know what YouTube is…

public class SwapPrefabsWithButtonPress : MonoBehaviour
{
    // click and drag the prefabs into these slots in inspector
    public GameObject prefabOneIWishToSwap;
    public GameObject prefabTwoThatDoesTheSwapping;

    void Update()
    {
         if (Input.GetKeyDown(KeyCode.space))
         { SwapThem(); }
    }

void SwapThem()
{
     if (prefabOneIWishToSwap.activeInHierarchy)
     { 
          prefabOneIWishToSwap.SetActive(false);
          prefabTwoThatDoesTheSwapping.SetActive(true);
          prefabTwoThatDoesTheSwapping.transform.position = Vector3.zero;
     }
     else
     {
          prefabOneIWishToSwap.SetActive(true);
          prefabTwoThatDoesTheSwapping.SetActive(false);
          prefabOneIWishToSwap.transform.position = Vector3.zero;
     }
}

And there you go, have fun pressing the space bar :slight_smile: