- Home /
Having game start with number of players selected
I am using Unity 2d and C#
I can't seem to find a tutorial with how to create the player that is already in my prefab folder onto the level without the character starting there. So with no code, getting to the level will have no players and with the code, getting to the level will have two players.
For example, if you select 2 players, the game will start with 2 players but before the game starts, you pick the game mode. I am thinking of making a separate script for the 2 players button which will load the game mode screen after clicking it.
Will it go in the same place as my load scene code or put it in void start or void update?
I might not really understand your question. So you want the user first choose how many player are going to play. Then choose a gamemode and after that the game start with the gamemode and amount of players
like this Choose player screen > choose gamemode > level start with amount of player from the choose players screen
Answer by aardappel156 · Nov 23, 2018 at 11:26 PM
What you could do is put on a script that saves the numbers of player and then adds this to start
DontDestroyOnLoad(this.gameObject);
here is the documentation
edit:
there is also this you could do this requires no extra script and might be cleaner for you.
//set amount of players
PlayerPrefs.SetInt("amountPlayers", 2);
//return int in amountPlayers
PlayerPrefs.GetInt("amountPlayers");
the 2 could be an int
for example
int amountPlayer;
amountPlayer = 2;
PlayerPrefs.SetInt("amountPlayers",amountPlayer);
the annoying thing with playerprefs is that it will save it forever so when you are testing make sure to have something like this ready(and delete it after you are done)
void Update()
{
//if you press delete you delete all the playerprefs
if (Input.GetKey(KeyCode.Delete))
{
PlayerPrefs.DeleteAll();
}
}
^^ I made a short imgur tutorial
Thanks I will try the second one.
Should I make a new script for it and just add that script where it needs to be added or load up the "number of players" or "game mode" script and add it there?
And yeah, I noticed that in play mode, unity saves all the information until you exit play mode and go back in it so I had to reset everything via code.
I would recommend the script where the player chooses the number of players. so when the player chooses the number also put that value and set the playerpref.
When you say "playerpref" thats what I named it in my prefs folder right? so I would replace "playerpref" with the name I gave it?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to randomly instantiate other prefabs parallel? 1 Answer
Scrolling level Instantiate after cycle 0 Answers
Optimisation for 3D level creation 0 Answers