- Home /
AddComponent(ScriptN) based on what PlayerPrefs has
We choose a type of character, "Character A", this loads Character A's inventory script (which is different from all other Character scripts (from B-Z). In Character A's inventory script (which has save and exit GUILayoutButtons) we click "Save" doing "something" with PlayerPrefs to only load Character A's script whenever we click Character A.
God I hope someone out there understands as I cannot figure out this easy concept..Here is some code to hopefully better convey the idea:
public class ChooseCharacter : MonoBehaviour {
// You'll notice that we load the Inventory script regardless of the decision
void OnGUI() {
if (GUI.Button (new Rect (600,100,200,20), "Character A")) {
Application.LoadLevel("Inventory");
}
if (GUI.Button (new Rect (600,160,200,20), "Character B")) {
Application.LoadLevel("Inventory");
}
if (GUI.Button (new Rect (600,220,200,20), "Character C")) {
Application.LoadLevel("Inventory");
}
if (GUI.Button (new Rect (600,280,200,20), "Character D")) {
Application.LoadLevel("Inventory");
}
}
}
So now with the Inventory Scene Loaded (which has the inventory script attached), it needs to make the distinction between character choices (from A-Z). So that when we click a character that we created, it shows its own inventory.
public class Inventory : MonoBehaviour {
void Start() {
if (we chose character A) { // use get Player Prefs but how?
// DON'T LOAD ANY OTHER SCRIPTS!
gameObject.AddComponent(typeof(CharacterA));
}
if (we chose character B) { // use get Player Prefs but how?
// DON'T LOAD ANY OTHER SCRIPTS!
gameObject.AddComponent(typeof(CharacterB));
}
if (we chose character C) { // use get Player Prefs but how?
// DON'T LOAD ANY OTHER SCRIPTS!
gameObject.AddComponent(typeof(CharacterC));
}
if (we chose character D) { // use get Player Prefs but how?
// DON'T LOAD ANY OTHER SCRIPTS!
gameObject.AddComponent(typeof(CharacterD));
}
if (we chose character E) { // use get Player Prefs but how?
// DON'T LOAD ANY OTHER SCRIPTS!
gameObject.AddComponent(typeof(CharacterE));
}
}
}
Here are the incredably shortend character scripts, in which the method "Save" needs to "Set" player prefs to something very specific so that the Inventory can load it and NOT get confused to the other character scripts..
public class CharacterA : MonoBehaviour {
public void Save() {
// Set Player Prefs to Something Specific for Char A!
// (MAKE DISTINCTION IN INVENTORY LOADER SCRIPT)
}
}
public class CharacterB : MonoBehaviour {
public void Save() {
// Set Player Prefs to Something Specific for Char B!
// (MAKE DISTINCTION IN INVENTORY LOADER SCRIPT)
}
}
public class CharacterC : MonoBehaviour {
public void Save() {
// Set Player Prefs to Something Specific for Char C!
// (MAKE DISTINCTION IN INVENTORY LOADER SCRIPT)
}
}
public class CharacterD : MonoBehaviour {
public void Save() {
// Set Player Prefs to Something Specific for Char D!
// (MAKE DISTINCTION IN INVENTORY LOADER SCRIPT)
}
}
public class CharacterE : MonoBehaviour {
public void Save() {
// Set Player Prefs to Something Specific for Char E!
// (MAKE DISTINCTION IN INVENTORY LOADER SCRIPT)
}
}
When I create a character and try to repeat that process, my original character (1st) is overwritten with the 2nd. If I try again, both characters are overwritten with the 3rd, and so on. I'm having a wild of a time trying to make the distinction..So I am befuddled and need some serious help.
I'm just trying to understand the problem here: Is your problem that you don't know how to do the "we chose character A" etc.. part?
Yes. Whats happening is that I am overwritten the original character I create with a new character.
I cant just do: userChar = 1 (for char A), userChar = 2 (for char B), userChar = 3 (for char C) and so on because it won't remember! That's why I think I need to set and get PlayerPrefs.
Answer by Atnas1010 · Nov 04, 2010 at 04:17 AM
What if you set the ChooseCharacter script to have a:
void Start () {
DontDestroyOnLoad(this);
}
and then set the userChar before you load the Inventory? And then you could get the ChooseCharacter script from the Inventory script and get the userChar the user picked?
Or am I not getting the problem?
I posted prematurely, please see if my post makes a little more sense now
Displaying the appropriate inventory is dependent on the inventory script.
I might need to rethink the whole selecting, loading, saving, and creating concepts. Is there an example of creating and selecting different characters?
I think we are really talking past each other. If I knew more c# I would post some code. You would load the Inventory level just like you do now, but in the Inventory script replace "we chose character A" with "gameObject.GetComponent(ChooseCharacter).userChar==0" (assu$$anonymous$$g userChar is public, and that they run on the same gameObject) "Or am I not getting the problem?"
Answer by user-6122 (google) · Nov 04, 2010 at 04:57 AM
Why don't you try setting a value in PlayerPrefs when you click the GUI button in inventroy. Something like this:
public class ChooseCharacter : MonoBehaviour {
void OnGUI() {
if (GUI.Button (new Rect (600,100,200,20), "Character A")) {
//we'll put the name of the character we want to modify here
//in PlayerPrefs
PlayerPrefs.SetString("Selected Character", "Character A");
Application.LoadLevel("Inventory");
}
Then, in your inventory code:
public class Inventory : MonoBehaviour {
void Start() {
//we can use PlayerPrefs this way, maybe...
if (PlayerPrefs.GetString("Selected Character") == "Character A") {
gameObject.AddComponent(typeof(CharacterA));
//optional, but just for good measure
PlayerPrefs.GetString("Selected Character") == "";
}
As for the last part of your problem, that's the part I get the least. But you could set up character sheets in PlayerPrefs for each character type like so:
public class CharacterA : MonoBehaviour {
public void Save(int gold, string weapon, string name) {
PlayerPrefs.SetInt("CharA Gold", gold);
PlayerPrefs.SetString("CharA Name", name);
PlayerPrefs.SetString("CharA Weapon Held", weapon);
}
Is that what you're going for?
Yes, and I've tried that time and time again. It does not work.
Your answer
