- Home /
how can I improve my "character select" script?
so i made this very archaic and hard-coded "character select" system, it consist of saving the id of the character "skin" (which the player changes with the key arrows to match the visual representation) to the player prefs, so then in-game, the player character instantiates this "skin" at start reading the int saved previously and comparing it to a game object array (arranged by hand) also, the ui displays the current skin loaded like "current player: cat-lord" by manually setting what the int saved in player prefs is equals regarding the name. now, it works, and I'm pretty proud of it, buuuuuut... what will happen when 3 skins turn into 10? then 20??? I'm wondering if there is anyone among you who can walk me or point me to the right direction on how i should go about automating some of this process (if at all possible) or if there is a better method. thanks for reading!!! I know it's a lot to ask, but even if you know a small way to ease the load of setting everything by hand, it'd be much appreciated.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Showroom_Manager : MonoBehaviour {
public GUISkin skin;
public string ID;
public Vector3 distanceAmount;
public bool keyDown = false;
//shells
public int shellQuantity = 2;
public int currentShell = 0;
void Start()
{
PlayerPrefs.SetInt ("Choosen Shell", 0);
}
void Update ()
{
//right arrow command
if (Input.GetKeyDown (KeyCode.RightArrow))
{
if (keyDown == false && currentShell <= shellQuantity -1 )
{
transform.position += -distanceAmount;
keyDown = true;
currentShell += 1;
}
}
if (Input.GetKeyUp (KeyCode.RightArrow)) {
keyDown = false;
}
//left arrow command
if (Input.GetKeyDown (KeyCode.LeftArrow))
{
if (keyDown == false && currentShell >= 1)
{
transform.position += distanceAmount;
keyDown = true;
currentShell -= 1;
}
}
if (Input.GetKeyUp (KeyCode.LeftArrow)) {
keyDown = false;
}
//enter command
if (Input.GetKeyDown (KeyCode.Return))
{
if (keyDown == false)
{
keyDown = true;
PlayerPrefs.SetInt ("Choosen Shell", currentShell);
SceneManager.LoadScene("main menu");
}
}
if (Input.GetKeyUp (KeyCode.Return)) {
keyDown = false;
}
//manually setting the name displayed
PlayerPrefs.GetInt ("Choosen Shell");
if (currentShell == 0) {
ID = "No-Shell";
}
if (currentShell == 1) {
ID = "Spark";
}
if (currentShell == 2) {
ID = "Shade";
}
if (currentShell == 3) {
ID = "Kabutops";
}
}
void OnGUI()
{
GUI.skin = skin;
GUI.Label (new Rect(Screen.width - 350, 40, 100, 200), "Bank:" + (PlayerPrefs.GetInt("Coins Gathered")), skin.GetStyle ("Coin Counter"));
GUI.Label (new Rect(Screen.width - 350, 5, 100, 200), "Prototype: Standard " + ID + " v" + (PlayerPrefs.GetInt("Death Number:")), skin.GetStyle ("Death Counter"));
GUI.Label (new Rect(Screen.width /2-100, 40, 100, 200), "Press Enter To Accept", skin.GetStyle ("Press Enter"));
}
}
this is the player instantiating code:
public GameObject[] shells;
//shell instantiating
print (PlayerPrefs.GetInt("Choosen Shell"));
GameObject selectedshell = Instantiate(shells [(PlayerPrefs.GetInt ("Choosen Shell"))], new Vector3 (0,2,0), Quaternion.identity) as GameObject;
selectedshell.transform.parent = transform;
Answer by Toon_Werawat · Apr 15, 2016 at 07:50 AM
Here is suggestion. Let's use Auto-Property & Array in your code.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Showroom_Manager : MonoBehaviour {
public GUISkin skin;
public string ID;
public Vector3 distanceAmount;
//public bool keyDown = false;
//shells
//public int shellQuantity = 2; this will no longer need because you can use ids.Length - 1
//This is auto property.
private int shl = 0; //This is a part of auto-property
public int currentShell
{
get { return shl; }
set
{
if (value < 0) { value = ids.Length - 1; }
if (value > ids.Length - 1) { value = 0; }
if (shl != value)
{
transform.position += -distanceAmount;
ID = ids[value];
shl = value;
}
}
}
//New variable this variable can also modify in unty inspector
public string[] ids = new string[]
{
"No-Shell",
"Spark",
"Shade",
"Kabutops"
};
void Start()
{
PlayerPrefs.SetInt ("Choosen Shell", 0);
}
void Update ()
{
//right arrow command
if (Input.GetKeyDown (KeyCode.RightArrow))
{
currentShell += 1;
}
if (Input.GetKeyDown (Keycode.LeftArrow)
{
currentShell -= 1;
}
//enter command
if (Input.GetKeyDown (KeyCode.Return))
{
PlayerPrefs.SetInt ("Choosen Shell", currentShell);
PlayerPrefs.Save();
SceneManager.LoadScene("main menu");
}
}
}
void OnGUI()
{
GUI.skin = skin;
GUI.Label (new Rect(Screen.width - 350, 40, 100, 200), "Bank:" + (PlayerPrefs.GetInt("Coins Gathered")), skin.GetStyle ("Coin Counter"));
GUI.Label (new Rect(Screen.width - 350, 5, 100, 200), "Prototype: Standard " + ID + " v" + (PlayerPrefs.GetInt("Death Number:")), skin.GetStyle ("Death Counter"));
GUI.Label (new Rect(Screen.width /2-100, 40, 100, 200), "Press Enter To Accept", skin.GetStyle ("Press Enter"));
}
}
Awwww man, thank you so much! you literally saved the future of my game (and my sanity). thanks a lot!!!! wokrs like a gem and saved me loads of lines of code on my different menus, etc.
Your answer
Follow this Question
Related Questions
Can´t instantiate objects in list correctly 1 Answer
Changes to prefab instance generated from script aren't remembered 1 Answer
How to pass array variables from one script in one scene to another script in another scene? 2 Answers
Prefabs instantiated from an array are keeping their public int value 1 Answer
An Instantiated object to array problem 2 Answers