Choosing between 4 Characters and Saving Them

Hello,

Firstly, we are presented with a “Character Selecton” Scene which has 4 empty slots. In which we have to select 1/4 of these slots to create a character in order to be able to enter the “Tavern” Scene.

Its kinda setup like this:

“Create Character!” // 1st slot
“Create Character!” // 2nd slot
“Create Character!” // 3rd slot
“Create Character!” // 4th slot

Which is produced by the following code:

    public class CharacterSelection : MonoBehaviour {
        
        void OnGUI() {
            
            if (GUI.Button(new Rect(100, 200, 100, 100), Slot_1); {
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 240, 100, 100), Slot_2); {
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 280, 100, 100), Slot_3); {
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 320, 100, 100), Slot_4); {
                Application.LoadLevel("CharacterCreation");
            }
        }
    }

Obviously, regardless of which slot we choose, we are taken to the “CharacterCreation” Scene to choose between 4 character races: RaceA, RaceB, RaceC, and RaceD, which is kinda setup like this:

RaceA // 1st Possibility
RaceB // 2nd Possibility
RaceC // 3rd Possibility
RaceD // 4th Possibility

Which is produced by the following code:

    public class CreateCharacter : MonoBehaviour {
        
        void OnGUI() {
            
             if (GUI.Button(new Rect(100, 200, 100, 100), "Race A") {
                 PlayerPrefs.SetString("CharacterChoice", "RaceA");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 240, 100, 100), "Race B") {
                 PlayerPrefs.SetString("CharacterChoice", "RaceB");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 280, 100, 100), "Race C") {
                 PlayerPrefs.SetString("CharacterChoice", "RaceC");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 320, 100, 100), "Race D") {
                 PlayerPrefs.SetString("CharacterChoice", "RaceD");
                 Application.LoadLevel("Inventory");
             }
        }
    }

Obviously, regardless of which race we choose, we are taken to the inventory which loads the races script that we chose:

    public class Inventory : MonoBehaviour {
   
        void Start() {
            if (PlayerPrefs.GetString("CharacterSelection", "RaceA") == "RaceA") {
                PlayerPrefs.SetString("CharacterSelection", ""); // doesn't help
                gameObject.AddComponent(typeof(RaceA));
            }
            if (PlayerPrefs.GetString("CharacterSelection", "RaceB") == "RaceB") {
                PlayerPrefs.SetString("CharacterSelection", ""); // doesn't help
                gameObject.AddComponent(typeof(RaceB));
            }
            if (PlayerPrefs.GetString("CharacterSelection", "RaceC") == "RaceC") {
                PlayerPrefs.SetString("CharacterSelection", ""); // doesn't help
                gameObject.AddComponent(typeof(RaceC));
            }
            if (PlayerPrefs.GetString("CharacterSelection", "RaceD") == "RaceD") { 
                PlayerPrefs.SetString("CharacterSelection", ""); // doesn't help
                gameObject.AddComponent(typeof(RaceD));
            }
        }
    }

Whats happening, is that I am overwritten the original character I create. For example, if I create RaceA, then create RaceB, RaceA’s data will be overwritten with RaceB…If I try again and create RaceC, Race B’s data will be overwritten with Race C. (like pushing onto a stack, only you are erasing all previous data with each new push).

Also, each character script has a “save” and “exit” button that the user can click. I am wondering If I could do something with that so the computer can know which inventory to load at selection time?

    public class RaceA : MonoBehaviour {
    
        void OnGUI() {
        
             if (GUI.Button(new Rect(100, 200, 100, 100), "Save") {
                  // Set the string of the Race, and the Slot we created it in!
             }
        }    
    }
    public class RaceB : MonoBehaviour {
    
        void OnGUI() {
        
             if (GUI.Button(new Rect(100, 200, 100, 100), "Save") {
                  // Set the string of the Race, and the Slot we created it in!
             }
        }    
    }
    public class RaceC : MonoBehaviour {
    
        void OnGUI() {
        
             if (GUI.Button(new Rect(100, 200, 100, 100), "Save") {
                  // Set the string of the Race, and the Slot we created it in!
             }
        }
    public class RaceD : MonoBehaviour {
    
        void OnGUI() {
        
             if (GUI.Button(new Rect(100, 200, 100, 100), "Save") {
                 // Set the string of the Race, and the Slot we created it in!
             }
        }    
    }

How about this?

    public class CharacterSelection : MonoBehaviour {
        
        void OnGUI() {
            
            if (GUI.Button(new Rect(100, 200, 100, 100), Slot_1); {
                PlayerPrefs.SetString("currentSlot", "Slot1");
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 240, 100, 100), Slot_2); {
                PlayerPrefs.SetString("currentSlot", "Slot2");
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 280, 100, 100), Slot_3); {
                PlayerPrefs.SetString("currentSlot", "Slot3");
                Application.LoadLevel("CharacterCreation");
            }
            if (GUI.Button(new Rect(100, 320, 100, 100), Slot_4); {
                PlayerPrefs.SetString("currentSlot", "Slot4");
                Application.LoadLevel("CharacterCreation");
            }
        }
    }

And then

    public class CreateCharacter : MonoBehaviour {
        
        void OnGUI() {
            
             if (GUI.Button(new Rect(100, 200, 100, 100), "Race A") {
                 PlayerPrefs.SetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot"), "RaceA");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 240, 100, 100), "Race B") {
                 PlayerPrefs.SetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot"), "RaceB");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 280, 100, 100), "Race C") {
                 PlayerPrefs.SetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot"), "RaceC");
                 Application.LoadLevel("Inventory");
             }
             if (GUI.Button(new Rect(100, 320, 100, 100), "Race D") {
                 PlayerPrefs.SetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot"), "RaceD");
                 Application.LoadLevel("Inventory");
             }
        }
    }

And then

    public class Inventory : MonoBehaviour {
   
        void Start() {
            if (PlayerPrefs.GetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot")) == "RaceA") {
                gameObject.AddComponent(typeof(RaceA));
            }
            if ((PlayerPrefs.GetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot")) == "RaceB") {
                gameObject.AddComponent(typeof(RaceB));
            }
            if ((PlayerPrefs.GetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot")) == "RaceC") {
                gameObject.AddComponent(typeof(RaceC));
            }
            if ((PlayerPrefs.GetString("CharacterChoice"+PlayerPrefs.GetString("currentSlot")) == "RaceD") { 
                gameObject.AddComponent(typeof(RaceD));
            }
        }
    }

Do you get the concept I am using? (and does this even work?) I couldn’t test it, because your script gave me some compiler errors, and I don’t know c#.

I think that worked…the concatenation of strings and player prefs…thx…

Edit: Yup. I knew today that I also needed to know the slot…thanks for helping man this really makes it easier now.