Character selection input

Hi, I currently have included a selection of characters in my game project in my game, how do I enter a input using a button to choose the character and go to the game scene with the character I chose?

I use these 2 scripts:

public class CharacterList : MonoBehaviour {

	public Canvas selectCharacter;
	public Button previousMenu;
	public GameObject SceltaPersonaggio;

	// Use this for initialization
	void Start () {
		SceltaPersonaggio.SetActive (true);
		selectCharacter = selectCharacter.GetComponent<Canvas>();
		previousMenu.enabled = true; 
		selectCharacter.enabled = true;

	}

	public void PreviousMenu()  //questa funzione è usata per nuova partita

	{

		{
			SceneManager.LoadScene(0);
		}

	}
}

And this one here:

public class CharacterSelector : MonoBehaviour {

public List<GameObject> characterList;
public int index=0;

void Start () {
	GameObject[] characters = Resources.LoadAll<GameObject>("Prefab");
	foreach (GameObject c in characters) {

		GameObject _char = Instantiate(c) as GameObject;
		_char.name = c.name;
		_char.transform.SetParent(GameObject.Find("CharacterList").transform);

		characterList.Add(_char);
		_char.SetActive(false);
		characterList[index].SetActive(true);

	}
}
public void Next(){
	characterList [index].SetActive (false);
	if (index == characterList.Count - 1) {
		index = 0;
	} else {
		index++;
	}
	characterList [index].SetActive (true);
	
}
public void Previous(){
	characterList [index].SetActive (false);
	if (index == 0) {
		index = characterList.Count-1;
	} else {
		index--;
	}
	characterList [index].SetActive (true);
	
}

void Update(){
	characterList [index].transform.Rotate (0,0.5f,0);
}

}

Thank you :slight_smile:

There are a couple ways to accomplish this. One simple way would be to use a static variable in your CharacterSelector screen.

public class CharacterSelector : MonoBehaviour {
    public static string selectedCharacter;

Then I would break out your activation into a new method like this

private void SelectCharacter(int index) {    
    characterList[index].SetActive(true);
    selectedCharacter = characterList[index].name;
}

Then replace both instances of

characterList [index].SetActive (true);

with

SelectCharacter(index);

Then when you load your new scene you can use

Resources.Load<GameObject>(string.format("Prefab/{0}", CharacterSelector.selectedCharacter));

to get the resource and then instantiate that GameObject

I tried but even if I click on the character I do not bring me to the scene of the game, what’s wrong, the code now and this here, made the changes you told me, what’s wrong?

public class CharacterSelector : MonoBehaviour {

	public static string selectedCharacter;
	public List<GameObject> characterList;
	public int index=0;

	void Start () {
		GameObject[] characters = Resources.LoadAll<GameObject>("Prefab");
		foreach (GameObject c in characters) {

			GameObject _char = Instantiate(c) as GameObject;
			_char.name = c.name;
			_char.transform.SetParent(GameObject.Find("CharacterList").transform);

			characterList.Add(_char);
			_char.SetActive(false);
			SelectCharacter(index);

			Resources.Load<GameObject>(string.Format("", CharacterSelector.selectedCharacter));
		}
			
	}

	private void SelectCharacter(int index) {    
		characterList[index].SetActive(true);
		selectedCharacter = characterList[index].name;
	}

	public void Next(){
		characterList [index].SetActive (false);
		if (index == characterList.Count - 1) {
			index = 0;
		} else {
			index++;
		}
		characterList [index].SetActive (true);
		
	}
	public void Previous(){
		characterList [index].SetActive (false);
		if (index == 0) {
			index = characterList.Count-1;
		} else {
			index--;
		}
		characterList [index].SetActive (true);
		
	}

	void Update(){
		characterList [index].transform.Rotate (0,0.5f,0);
	}
}