Hey everyone! I am working on a mobile game at the moment and before I get to deep I was wondering what would the best way to do character selecting and loading?
Should I store an int in playerprefs and have all the possible characters in a gameobject and then on level load grab the playerpref int and then set active that character or would this take too much time and slow down the game?
Thanks in advance!
It wouldn’t take that much time. You probably wouldn’t notice it…Does depend on what you’re loading, but this simple operation wouldn’t create problems.
Prefabs? Yes def…
There are many ways to structure your game system. But here is one simple way.
You make a list of gameobjects, drag each prefabs in to the list, and make UI selection that browes
each character.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class NewBehaviourScript : MonoBehaviour {
public GameObject SelectedPlayer;
public List<GameObject> CharList = new List<GameObject>();
public void SelectCharacter()
{
GameObject select = CharList.FirstOrDefault(l => l == SelectedPlayer);
if (select != null)
Instantiate(select);
DontDestroyOnLoad(select)
// Load level
}
}
@Ironmax Then when I load up the level I just instantiate the index that was selected from the list? Yeah? Hope I am understanding it lol
Also I am not understanding this syntax right here:var select = CharList.FirstorDefualt(l => l.GameObject == SelectedPlayer);
I have changed the code to be more correct for you. No you dont need any index, since your not dealing with array or integer. Serializing will deal with the comparison for you. I am showing you Linq with Lamda Expression to simply query your gameobject list.
When you Instantiate the selectet Character you can load your level.
Make also sure you do DontDestroyOnLoad(Select); first.
Thank you very much @Ironmax
your welcome
have a great summer