-1

Hello I am pretty new to Unity and I would like some help. Here is how the scenes in my game are set up so far:

  • Main Menu
  • Character Selection
  • Level Selection (UI MAP with locked and unlocked levels)
  • Level 1

I watched a tutorial on creating the character selection option and saving the chosen character, then bringing it to the next scene. The problem is that I want it to only appear in the "Level 1" scene, and not in the Level selection Scene/Menu. How would I do this?

These are the lines of code that I used to save the character so far:

 public void PlayGame()
    {
        PrefabUtility.SaveAsPrefabAsset(playerSkin, "Assets/Artwork/characters/selectedSkin.prefab");
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
CorrieJanse
  • 2,374
  • 1
  • 6
  • 23
  • I am thinking something simpler.... Why dont you use playerPrefs https://docs.unity3d.com/ScriptReference/PlayerPrefs.html ? Either that; or perhaps you can just deactivate your char in the levelselectionscene. – Hikari Nov 16 '21 at 08:37

1 Answers1

1

If you are new maybe use DontDestroyOnLoad() on an player object that saves your skin, skills etc. instead of saving your player as a prefab. This leaves this player object as an object instantiated when loading a new scene and you can just copy the values to the player that you want to use

//Check this on another player that you want to save the values to
public bool usedAsSave;
void Start()
{
    if(usedAsSave)
    {
        //Disable the SpriteRenderer so it's not drawn and the Rigidbody2d that he doesn't fall
        GetComponent<SpriteRenderer>().enabled = false;
        GetComponent<Rigidbody2D>().enabled = false;
        DontDestroyOnLoad(this.gameObject);
    }
}    

_________________________________________________________________________

//This is on what ever object you are calling it from
void PlayGame()
{
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}

//Now you can call this in whatever scene you want the player to be by, for example binding it into a if statement
//if(SceneManager.GetActiveScene().buildIndex == 1) fror example
void CopyPlayer()
{
    //In this example the object has the name "SaveObejct"
    var copy = GameObject.Find("SaveObject");

    //Now you can copy all the values to the Player you are using
    playerSkin.whateverValue = copy.whateverValue;
}

Here is the DontDestroyOnLoad API from Unity.

This is a bit more writing but I find easier to understand for beginners and I used this in the beginning too, to transfer data between scenes. I hope understood you properly, if not please explain the question a bit more