Choosing a player?

Say I have 2 or 3 or 4 different character meshes the player can choose from before they start the game.

Would I just duplicate the level however many times as different players and load that characters level when they push the button to choose- or is there a better way to do this…for android??

and why don’t you save player(-s) in prefabs?

All my character meshes are prefabs…

Also, use PlayerPrefs. (:

How???

Use Instanciate ?

Can you elaborate???

If you have four different characters, and they are created as prefabs, you would set up a script that has a reference to all of those prefabs stored in an array. Then when you start your game, you can figure out which character they want to play, and use the Instantiate function to create a clone of the prefab.

Something like this (note: untested code):

var characters[];
var characterChosen : int;
var playerCharacter;
function Start()
{
   playerCharacter = Instantiate(characters[characterChosen], Vector3.zero, Quaternion.identity);
}

Set the array of characters to refer to your prefabs, then, at level load, you set the characterChosen integer to the index of the character you want to play.

I understand it a little better- I do have them as prefabs so I would give them the int value1-4 for each of them and then that value would load the right one at run time??

Is this the same in C# sept for the function

Not sure I get the array deal-

1 Like

Ah, okay, sorry, C# is easy enough to convert to. For some reason I thought you were using UnityScript.

class SelectPlayer: MonoBehaviour
{
  public GameObject[] PossibleCharacters;
  public int SelectedCharacterIndex;

  [HideInInspector]
  public GameObject playerCharacter;
  void Start()
  {
    playerCharacter = Instantiate(PossibleCharacters[SelectedCharacterIndex], Vector3.zero, Quaternion.identity);
  }

}

Where would I put this code at In the camera where my main menu is???

Hmm, that will depend on how you handle your character selection. If you DO NOT perform a level load between character selection screen and playing the game, you can attach that script anywhere you like, camera could easily work for it, or perhaps put it on whatever game object you have designated as your game controller. If you load a new level between character selection and playing the game, you either want to save which character was selected in to player prefs so that it persists through the level load, or attach the script to an object that is flagged DontDestroyOnLoad.

Wow, I am opening up a can of worms now- I don’t even know what playerprefs are or what they are for yet…

Ok Justin I appreciate the insight, I need to get on this playerpref deal eh?

Probably not.

I would set the character select to be in the same scene as the main game for now, later on, solve the problem of how you get the player’s selection of character to play with in a menu scene passing through, but it’s not a problem you need to solve right now. The simplest solution, when you get that far, would actually be to create a persistent object in your character select scene that remembers the index of the character selected, and then in your game play scene, you just find that game object, then ask it what the selected character index was.

Here’s a quick character selection screen I just threw together. It displays buttons based on the number of pre-defined characters. I created four nefarious prefab characters we shall introduce as Mr Sphere, Mr Cube, Mr Cylinder and Dr Capsule. The first scene to load is the character select scene, upon selecting your hero, the game play scene loads, with the character you have selected.

All code and designs for this character selection are released to the public domain so you are free to do with them as you please.

731196–26588–$CharacterSelection.unitypackage (37.1 KB)

Thanks Justin I will be figuring this out today, I need to wrap my head around this. Thanks for pointing me in the right direction in doing this-

Justin-

I’ve been working with this character select package, I see what it is suppose to do, but it isn’t picking the players.

Not sure if I am doing it right thou. My players are prefabs and if I do not put them int he scene nothing loads- If I put them both in the scene they both load no matter what.

I’m I missing something??

Anyone have clues why this won’t work???

This is what I am trying since I can’t figure the above out-

void OnGUI () 
	{
		GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), BackGround);
		      GUI.color = Color.green;
	      if (GUI.Button(new Rect(30, 100, 200, 50), "Fly the F-35 JSF"))
		    {
			Instantiate(JSFPlayer, transform.position, transform.rotation);
			AudioSource.PlayClipAtPoint(clip,currentListener.position);
			Application.LoadLevel(1);
		    }
		if (GUI.Button(new Rect(30, 200, 200, 50), "F-16E"))
		    {
			Instantiate(FightingFalconPlayer, transform.position, transform.rotation);
			AudioSource.PlayClipAtPoint(clip,currentListener.position);
			Application.LoadLevel(1);
		    }
	}

Why wouldn’t this work- this is in the main menu scene, then loads to the level but the Instantiation isn’t happening.

check if the characters inst null. also you instantiating , then making the game load a different level without you char. the instantiating is working, its just getting destroyed when you switch. so you need to instantiate after the level is loaded. to do this you need you will need to make sure this script or object isn’t destroyed when you change scenes, to do this you add the DontDestroyOnLoad() function to one of your methods ( preferably start or update). then you want to make a variable that will store your selected character. and make it store that character into the variable when you hit those buttons you defined. then load the next level, now call a function after its done to make it check what level your on(using if statements) if true instantiated the character stored in your variable. and that should work :slight_smile: