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!
}
}
}