Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 AnUnVi · Aug 23, 2018 at 07:57 PM · tutorialsave datainventory systemcombineimplementation

Combining two systems: Adding Inventory to Save Data

So my problem is a bit of a specific one.


I'm pretty inexperienced when it comes to programming, and I followed two separate tutorials to implement the save and inventory systems for my game: these two articles for saving and loading, and this tutorial for creating an inventory.


Both systems seem pretty good to me - my main problem is that I don't know how to feed the values from the inventory system into the save system (because, again, inexperienced).


Here's the Inventory file:

 public class Inventory : MonoBehaviour {
 
     public Image[] itemImages = new Image[numItemSlots];
     public Item[] items = new Item[numItemSlots];
     public const int numItemSlots = 16;
     public void AddItem (Item itemToAdd) {
 
         for (int i = 0; i < items.Length; i++) {
             if (items[i] == null){
                 items[i] = itemToAdd;
                 itemImages[i].sprite = itemToAdd.sprite;
                 itemImages[i].enabled = true;
                 return;
             }
 
         }
     }
 
     public void RemoveItem (Item itemToRemove)
     {
         for (int i = 0; i < items.Length; i++) {
             if (items[i] == itemToRemove) {
                 items[i] = null;
                 itemImages[i].sprite = null;
                 itemImages[i].enabled = false;
                 return;
             }
         }
     }
 }

And here's the PlayerStats:

 [System.Serializable]
 
 public class PlayerStats {
 
     public int SceneID;
     public float PlayerPosX, PlayerPosY, PlayerPosZ;
 
     public float coinage;
     public ArrayList items;      //I added this preemptively because I thought it might help - it doesn't currently serve a purpose.
 }
  

And the PlayerControl:

    public class PlayerControl : MonoBehaviour
     {
         [HideInInspector]
         public bool facingRight = true;         // For determining which way the player is currently facing.
         [HideInInspector]
     
         public float moveForce = 365f;            // Amount of force added to move the player left and right.
         public float maxSpeed = 2f;                // The fastest the player can travel in the x axis.
         public bool isSitting = false;
         public GameObject inventory;
     
         private Transform groundCheck;            // A position marking where to check if the player is grounded.
         private bool grounded = false;            // Whether or not the player is grounded.
         private Animator anim;                    // Reference to the player's animator component.
     
         private void Start()
         {
             if (GlobalControl.Instance.IsSceneBeingLoaded)
             {
                 PlayerState.Instance.localPlayerData = GlobalControl.Instance.LocalCopyofData;
                 transform.position = new Vector3(
                     GlobalControl.Instance.LocalCopyofData.PlayerPosX,
                     GlobalControl.Instance.LocalCopyofData.PlayerPosY,
                     GlobalControl.Instance.LocalCopyofData.PlayerPosZ
                     + 0.1f);
                 GlobalControl.Instance.IsSceneBeingLoaded = false;
             }
         }
        
         void Awake()
         {
             // Setting up references.
             groundCheck = transform.Find("groundCheck");
             anim = GetComponent<Animator>();
             inventory = GameObject.Find("Canvas").GetComponent<OpenInventory>().Inventory;
         }
     
     
         void Update()
         {
             if (Input.GetKeyDown(KeyCode.F5))
             {
                 PlayerState.Instance.localPlayerData.SceneID = SceneManager.GetActiveScene().buildIndex;
                 PlayerState.Instance.localPlayerData.PlayerPosX = transform.position.x;
                 PlayerState.Instance.localPlayerData.PlayerPosY = transform.position.y;
     
                 GlobalControl.Instance.SaveData();
             }
     
             if (Input.GetKeyDown(KeyCode.F9))
             {
                 GlobalControl.Instance.LoadData();
                 GlobalControl.Instance.IsSceneBeingLoaded = true;
                 int whichScene = GlobalControl.Instance.LocalCopyofData.SceneID;
     
                 SceneManager.LoadSceneAsync(whichScene);
             }
         }
     
         //[...]
     }

I realize that there's a tutorial for saving droppable items and such immediately after the first two, but because I already have an existing Inventory, I'm not exactly sure how much it applies/how much of the work would be necessary.


I feel like it's just a matter of taking the right values from Inventory and putting them in PlayerStats and PlayerControl with the right setup - plugging in the wires, so to speak. Problem is, I don't know where to start.


Can anyone help me?

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

0 Replies

· Add your reply
  • Sort: 

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

92 People are following this question.

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

Related Questions

Help in Gesture Implementation ... 1 Answer

Saving a class List from another script 0 Answers

I need to find inventory and item system, and to save them 0 Answers

Adventure game tutorial (can't use items) 0 Answers

Save and load an inventory 0 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