Reputation: 77
I am trying to make a character select screen in Unity. But I don't know how I can tell the game scene what skin got picked in the character select scene.
I have tried using DontDestroyOnLoad();
but didn't get it to work.
I need to send the value from a canvas in one scene to a player object in the next scene. I didn't get a player prefab to work because many of the values the player need only exists in the game scene.
I am not much experienced with Unity or C#, so I would appreciate if you give very detailed answers and explain to me what different things do and why.
Lastly, I am sorry if this question is hard to understand, as I said, I'm not that experienced. I am also not that good in English. Thank you for your time.
Upvotes: 0
Views: 129
Reputation: 1060
You can use design pattern Singleton to create an unique instance.
public class CharacterManager
{
private static CharacterManager singleton;
private Skin currentSkin;
public static CharacterManager Singleton
{
get
{
if (singleton == null)
{
singleton = new CharacterManager();
}
return singleton;
}
}
public void SelectSkin(Skin skin)
{
currentSkin = skin;
}
public Skin GetCurrentSkin()
{
return currentSkin;
}
}
Usage:
Select skin in Scene1
CharacterManager.Singleton.SelectSkin(someSkin);
Get current skin in Scene2
character.skin = CharacterManager.Singleton.GetCurrentSkin();
Upvotes: 0
Reputation: 334
You may use PlayerPrefs for it (as a variant instead of DontDestroyOnLoad(gameObject)
): https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
Upvotes: 2