Character selection

Hi,

I’m a new programmer, trying to create a selection screen that allows you to pick a character and deploy it in the next level.
From what I’ve read , I have to create an array of characters:

//I think it's like this...
var Characters = new Array();
Characters[1] : GameObject;
Characters[2] : GameObject;
Characters[3] : GameObject;

But then I don’t know where to go from there. How would I go about making a script that remembers what character you’ve picked and then deploys it.
Snippets of code would be brilliant as well as example projects(if anyone’s willing to share one with me).

Thanks,
Hawx


During writing the above , I came up with this:

var Spawn : Transform;
var Character : GameObject;

function OnMouseDown () {
    Debug.log("Player Selected!");
      Application.LoadLevel (Application.loadedLevel + 1);
      Instantiate(this, Spawn.transform.position, transform.rotation );

}

The above code hopefully does what I want it to do(load a selected character in another level). I haven’t tested it yet, so if a good programmer could read over it for any faults, that would be brilliant.

Thanks,
Hawx

Not sure if that will work or not with scene transition (Once the Application.LoadLevel is called I dont think it will get to the next line - if it does it would probably be just loaded in the previous scene then unloaded straight away) but an easy workaround is have the array of characters saved in your next scene and either:

  1. Set up an empty object in your initial character selection screen and create a new global game state script that keeps track of stuff you need (such as which character the player has chosen) and use Unity - Scripting API: Object.DontDestroyOnLoad to keep it there in the next scene (then you can read the player’s choice and instantiate it from there
  2. Same theory but use Unity - Scripting API: PlayerPrefs.SetInt and Unity - Scripting API: PlayerPrefs.GetInt to keep track of the player’s choice. This will let you save the game too
  3. Combine the above! Have the global game state load the saved prefs with GetInt (if applicable) and write over them/save before quitting with SetInt

Thanks very much , will try this.

So , something like this?

var Character : GameObject;

function OnMouseEnter(){
renderer.material.color = Color(1,0,0);
}

function OnMouseExit(){
renderer.material.color = Color(1,1,1);
}

function OnMouseDown () {
      Application.LoadLevel (Application.loadedLevel + 1);
        DontDestroyOnLoad (Character);
      Instantiate(Character, Vector3 (1, 2, 0), Quaternion.identity);
      

}

I’ve had a play around with this and I’m not getting it. Would someone be kind enough to post some code showing me the way?

I’ve got this:

var Character : GameObject;

function OnMouseEnter(){
renderer.material.color = Color(1,0,0);
}

function OnMouseExit(){
renderer.material.color = Color(1,1,1);
}

function OnMouseDown () {
      Application.LoadLevel (Application.loadedLevel + 1);
        DontDestroyOnLoad (Character);
      Instantiate(Character, Vector3 (-969.2523, 37.40265, -94.34366), Quaternion.identity);
      

}

But this isn’t working. Does anyone have any other ideas on how to make this work?
A sample of code would be helpful.

Thanks,
Hawx

I would take another approach.

Make a menu where you can pick what character to play with (start with simple GUI labels).

Save the name of the characters prefab using PlayerPrefs.SetString().

When you load a scene you can in the Start() method then use Resources.Load(PlayerPrefs.GetString()) to obtain the chosen character. Remember that the prefab needs to be in a resources folder to work:

//character prefabs
public var character0:Transform;
public var character1:Transform;
public var character2:Transform;

//you can change this in editor or from script to load a different character
public var characterChoice:Int = 0;

function Awake () {
    //with this, the gameobject you attached the script not gonna get destroyed after loading
    DontDestroyOnLoad (transform.gameObject);
}


function OnLevelWasLoaded (level : int) {
     if(characterChoice == 0){
      Instantiate(character0, Vector3 (-969.2523, 37.40265, -94.34366), Quaternion.identity);

     }
     else if(characterChoice == 1){
      Instantiate(character1, Vector3 (-969.2523, 37.40265, -94.34366), Quaternion.identity);
     }
     else if(characterChoice == 2){
      Instantiate(character2, Vector3 (-969.2523, 37.40265, -94.34366), Quaternion.identity);
     }
//etc if you have more options
}

function OnMouseDown () {
      Application.LoadLevel ("scene");  //the name of your scene you want to load goes here 

}

You attach this script to a gameobject in your starting scene, if you get some compiler errors, i am sure you can fix it, but this should work.

Thanks to both of you , I’ll try each method. :slight_smile: