Character selection (PlayerPrefs.GetInt) problem

hi there,

      i am making a character selection and character spawner script. 

here is character selection script–>


using UnityEngine;
using System.Collections;

public class CharacterSelect : MonoBehaviour {
// script link → Unity - Scripting API: RaycastHit2D.collider
public int SelectedPlayer = 0;

void Update()
{
	//If the left mouse button is clicked.
	if (Input.GetMouseButtonDown(0))
	{
		//Get the mouse position on the screen and send a raycast into the game world from that position.
		Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
	    RaycastHit2D hit = Physics2D.Raycast(worldPoint,Vector2.zero);
		
		// if it hits a collider and the objects name is Player1, then Selected character is this object
		if(hit.collider.name == "Player1"){
			SelectedCharacter1();
			}
		// if it hits a collider and the objects name is Player1, then Selected character is this object
		if(hit.collider.name == "Player2"){
			SelectedCharacter2();
			}
		// if it hits a collider and the objects name is Player1, then Selected character is this object
		if(hit.collider.name == "Player3"){
			SelectedCharacter3();
		}
		// if it hits a collider and the objects name is Player1, then Selected character is this object
		if(hit.collider.name == "Player4"){
			SelectedCharacter4();
		}
	}
}
// methods for the three selected character
void  SelectedCharacter1 (){
	Debug.Log ("SHIP 1 SELECTED"); //Print out in the Unity console which character was selected.
	SelectedPlayer = 1;
	PlayerPrefs.SetInt("selectedPlayer", (SelectedPlayer));
}

void  SelectedCharacter2 (){
	Debug.Log ("SHIP 2 SELECTED");
	SelectedPlayer = 2;
	PlayerPrefs.SetInt("selectedPlayer", (SelectedPlayer));
}
void  SelectedCharacter3 (){
	Debug.Log ("SHIP 3 SELECTED");
	SelectedPlayer =3;
	PlayerPrefs.SetInt("selectedPlayer", (SelectedPlayer));
}
void  SelectedCharacter4 (){
	Debug.Log ("SHIP 4 SELECTED");
	SelectedPlayer = 4;
	PlayerPrefs.SetInt("selectedPlayer", (SelectedPlayer));
}

}


Here is the character spawner script →


using UnityEngine;
using System.Collections;

public class CharacterSpawner : MonoBehaviour {
// Empty Gameobject slots
//these are the player prefabs that will be automagically plugged in for us.
public GameObject player01Prefab;
public GameObject player02Prefab;
public GameObject player03Prefab;
public GameObject player04Prefab;

//this is where the script placed in the level inputs in this number for the player who was selected
//and saved by playerPrefs
// This will grab the information from the Character Select script and put it in here
public int savedPlayer = 0;


//this is called first before the Start function, so make sure it loads everthing needed first
void Awake () {
	savedPlayer = PlayerPrefs.GetInt("SelectedPlayer");
	
	player01Prefab = GameObject.Find("Player1");
	player02Prefab = GameObject.Find("Player2");
	player03Prefab = GameObject.Find("Player3");
	player04Prefab = GameObject.Find("Player4");
	
	if(savedPlayer == 0) //if we've not selected any player initially lets just use Player 1 
	{  					
		player01Prefab.SetActive(true);
		player02Prefab.SetActive(false);
		player03Prefab.SetActive(false);
		player04Prefab.SetActive(false);
	}
	else if(savedPlayer == 1) //if we've set the player to 1 from playerprefs then 
	{  					
		player01Prefab.SetActive(true);
		player02Prefab.SetActive(false);
		player03Prefab.SetActive(false);
		player03Prefab.SetActive(false);
	}
	else if(savedPlayer == 2) //if we've set the player to 2 from playerprefs then 
	{  					
		player01Prefab.SetActive(false);
		player02Prefab.SetActive(true);
		player03Prefab.SetActive(false);
		player03Prefab.SetActive(false);
	}
	else if(savedPlayer == 3) //if we've set the player to 3 from playerprefs then 
	{  					
		player01Prefab.SetActive(false);
		player02Prefab.SetActive(false);
		player03Prefab.SetActive(true);
		player03Prefab.SetActive(false);
		
	}
	
	else if(savedPlayer == 4) //if we've set the player to 3 from playerprefs then 
	{  					
		player01Prefab.SetActive(false);
		player02Prefab.SetActive(false);
		player03Prefab.SetActive(false);
		player03Prefab.SetActive(true);
		
	}
}

}


the character selection script is on the selection scene main camera. the character spawner script is in the game scene on the main camera.

the problem is that the main camera in game scene does not get the selected player from the previous scene and give the value of lets say 2. it always remains 0 in the inspector while the game is running.

i also have a prefab delete script which deletes every character except the one with selected character value.

  • another error was in the first script, line 18 -if(hit.collider.name == “Player1”)
    the error was- "Object reference not set to an instance of an object’

Please help

thanks in advance. :slight_smile:

In all of your PlayerPrefs.SetInt() method calls, you are passing “selectedPlayer” but passing “SelectedPlayer” (first letter capitalized) in the GetInt() call.

As for the Object Reference, hit.collider will be null if nothing was hit.

I think you have two character select 1 parts in your code. Not sure if that’s an issue. I am also having an issue with character selection. Best of luck, I’ll be watching this post.