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 JoshMBeyer · Aug 22, 2012 at 08:40 PM · playersavedatauserprofile

How can I make this script save and or load what I do in my game??

I need help, I am trying to just make a single player RPG very low quality and kinda similar to the older runescape. I can't seem to figure something out although. Right now I wrote a script so if I type in the AccountID field, "admin". And the Password field, "admin". And click "LogIn" Then it will do what I tell it. It works, I tested it and when I click Log in it replys to the console "ID & Password Are Correct." (I know how to Application.LoadLevel and all that) But I don't know how to make it to where while playing the game it's saving everything that changes when my XP goes up, if I pick up some new items in the game. So when I go back to the game, type in my info and click log in it loads the players saved/stored information ie; items, experience, etc. How would I go about doing this? Here is my current code for the part mentioned above, i left out a couple other parts like the other gui buttons, this is just an example.

PS. I don't care if i have to make more scripts than just one (Im sure I can't get what im wanting to do into just a single script.

 var stringToEdit : String = "AccountID";
 
 var passwordToEdit : String = "My Password";
 
 function OnGUI
 {
     // Make a text field that modifies stringToEdit.
     stringToEdit = GUI.TextField (Rect (260, 240, 136, 20), stringToEdit, 25);
     
     // Make a password field that modifies passwordToEdit.
     passwordToEdit = GUI.PasswordField (Rect (266, 262, 138, 20), passwordToEdit, "*"[0], 25);    
     
     if (GUI.Button (new Rect(288,340,98,22), "Log In")) //If "Log In" button is pressed.
     {
         if(stringToEdit == "admin" && passwordToEdit == "admin") //If The text field is equal to "admin" and the password field is equal to "admin".
         {
             print("ID & Password Are Corect.");
             //DoSomethingAlso
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BLarentis · Aug 23, 2012 at 03:54 PM

Hello again, it'll be something like this:

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour {
     
     public string[] itens = new string[20];
     
     // Use this for initialization
     void Start () {
     
         
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     void saveInventory(){
         
         //Lets consider that you have all your inventory listed inside a array (created at the start of the class)
         //The array size is the size of slots of your inventory
         
         //What you will have to do is to save all those itens inside the player prefs so you can access them every time that
         //need
         
         //It will be something like this
         for (int i = 0; i < itens.Length; i++) {
             
             //Here i create the name of the key
             string inventorySlot = "InventorySlot";
             //And here i change the name so it will access all the positions
             inventorySlot = inventorySlot + i.ToString();
             
             //And here i save to PlayerPrefs
             PlayerPrefs.SetString(inventorySlot,itens[i]);
             
             //Just remember that your array start to count on 0.
             //So if you have 20 positions, those positions will be 0-19
             //So the first slot will be InventorySlot0
             //The second will be InventorySlot1, and so on untill InventorySlot19
             
         }
         
     }
 }
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 JoshMBeyer · Aug 25, 2012 at 04:34 AM 0
Share

Oh okay ( i use javascript but you explained it really good so It makes just as much since. Thanks for the help

avatar image
0

Answer by BLarentis · Aug 22, 2012 at 09:56 PM

Hello, my advice for you is to study this:

http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html

This will create a file inside your application files, that will never be erased, unless you erase it by code or manually. Because of that you can use those files to save all your characters infos. Take care.

Comment
Add comment · Show 4 · 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 JoshMBeyer · Aug 22, 2012 at 10:01 PM 0
Share

Yes, those help alot I see how I can use them to store experience but how can I use them to save the Items in my inventory or bank in the game tho?

avatar image BLarentis · Aug 22, 2012 at 10:28 PM 0
Share

You can use the SetString method. And use a key like: PlayerPrefs.SetString("InventorySlot1","Item1"); PlayerPrefs.SetString("InventorySlot2","Item2"); and so on ... Then when you load your game you use a for to check all the Inventory $$anonymous$$eys. So you don't need to check player prefs all the time you open your inventory.

avatar image JoshMBeyer · Aug 22, 2012 at 11:00 PM 0
Share

Can someone write me an example script? I see that there are a couple different ones, but one is "Save" in order for them to work do they need the Save included in the script?

avatar image BLarentis · Aug 23, 2012 at 12:52 AM 0
Share

As soon as i finish the sample script i´ll post in here.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Save and Load a Game 1 Answer

Loading a list into unity 0 Answers

Is it possible to interact with a USB Drive? 0 Answers

saving data 1 Answer

How do you copy PersistentDataFolder between devices? 2 Answers


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