Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lampshade · Nov 04, 2010 at 03:07 AM · playerprefsloadingscenes

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Atnas1010 · Nov 04, 2010 at 03:17 AM 0
Share

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?

avatar image lampshade · Nov 04, 2010 at 03:29 AM 0
Share

Yes. Whats happening is that I am overwritten the original character I create with a new character.

avatar image lampshade · Nov 04, 2010 at 03:38 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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?

Comment
Add comment · Show 14 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lampshade · Nov 04, 2010 at 04:19 AM 0
Share

No that doesn't work because it is the same scene.

avatar image Atnas1010 · Nov 04, 2010 at 04:21 AM 0
Share

I posted prematurely, please see if my post makes a little more sense now

avatar image lampshade · Nov 04, 2010 at 04:28 AM 0
Share

Displaying the appropriate inventory is dependent on the inventory script.

avatar image lampshade · Nov 04, 2010 at 04:35 AM 0
Share

I might need to rethink the whole selecting, loading, saving, and creating concepts. Is there an example of creating and selecting different characters?

avatar image Atnas1010 · Nov 04, 2010 at 04:36 AM 0
Share

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?"

Show more comments
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lampshade · Nov 04, 2010 at 05:13 AM 0
Share

Yes, and I've tried that time and time again. It does not work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Saving boolean values using PlayerPrefs 1 Answer

Issue with PlayerPrefs not loading a value, as well as a bug... 1 Answer

Seamless Room Transitions 0 Answers

Saving and load from player prefs in Unity3D on mobile devices?? 0 Answers

Remembering Previously Loaded Scenes "Doors" 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges