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 /
This question was closed May 10, 2019 at 12:42 AM by edthered1009 for the following reason:

The question is answered, right answer was accepted

avatar image
-1
Question by edthered1009 · May 05, 2019 at 03:41 AM · inventory systemitemsstrangeinconsistent

Strange Persistence of Memory

 public class Item : MonoBehaviour
 {
     public int itemQuantity;
     [HideInInspector]
     public int quantity;
 
     private void Start()
     {
         quantity = itemQuantity;
     }
 
     public virtual void ActionKeyDown(KeyCode key, Inventory inv)
     {
 
     } 
 }
 

This is my Item class. When I run the game for the first time in the editor, it initializes with the correct quantity value (2). I use up all the of the item. Then, I quit and then run the game again. The quantity is still at 0.

Can someone please explain this strange behavior?

Comment
Add comment · Show 10
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 andrew-lukasik · May 05, 2019 at 08:55 AM 0
Share

You need property and not a public (serialized) field:

 public int quantity => itemQuantity;

If not it's bit confusing what you're trying to do here exactly

avatar image andrew-lukasik andrew-lukasik · May 05, 2019 at 08:58 AM 1
Share

$$anonymous$$y bet is that quantity, as a public field, is serialized as 0, and that's what you see.

avatar image edthered1009 andrew-lukasik · May 05, 2019 at 07:43 PM 0
Share

I don't think that's the solution, as I can no longer access the quantity from the inspector. Thanks! (Also, next time, could you integrate the code with my example?)

Show more comments
avatar image highpockets · May 05, 2019 at 10:21 PM 0
Share

Is itemQuantity set to 2 in the inspector?

avatar image edthered1009 highpockets · May 05, 2019 at 11:10 PM 0
Share

Yes, it is.

avatar image GilesDMiddleton · May 05, 2019 at 10:37 PM 1
Share

is another class able to write to either property? What I normally do here is try preventing access to the fields and controlling changes through getters/setter, that way you can easily debug what's happening in the set{}

e.g.

 [SerializeField]
 protected int itemQuantity;
 protected int quantity;
 
 public int CurrentQuantity 
 {
    get { return quantity; }
    set
    {
       // put a breakpoint here
      quantity = value;
    }
 }

avatar image edthered1009 · May 06, 2019 at 09:14 PM 0
Share

Sorry, but none of these are working. Every time, the items firmly remain at 0. I need help with this, because items are kinda critical in my game.

avatar image andrew-lukasik edthered1009 · May 07, 2019 at 09:06 AM 0
Share

You're complicating this for yourself. Stop serializing "quantity" field and make "itemQuantity" private+serialized man. If that won't help then you have a code that is influencing state of this object in code you're not showing - it's simple as that.

 NOTE: To inspect values of non-serializable private fields
         switch Inspector tab to Debug mode.

1 Reply

  • Sort: 
avatar image
-2
Best Answer

Answer by edthered1009 · May 10, 2019 at 12:42 AM

Nevermind, I figured it out by myself. Thanks for trying to help! :)

Edit: Here is the Item script.

 public int itemQuantity;
     private int quantity;
 
     public virtual void ActionKeyDown(KeyCode key, Inventory inv)
     {
         
     }
 
     public virtual void ActionKey(KeyCode key, Inventory inv)
     {
 
     }
 
     public void RemoveAmount(int amount)
     {
         if (quantity - amount > -1)
         {
             quantity -= amount;
         }
     }
 
     public void AddAmount(int amount)
     {
         quantity += amount;
     }
 
     public int GetQuantity()
     {
         return quantity;
     }


This is the Inventory script.

     public List<Item> inventory;
     public int index;
     [HideInInspector]
     public Item currentItem;
     public Text text;
     public Text quantityText;
     public KeyCode[] keys;
 
     private void Start()
     {
         index = 0;
         foreach (Item item in inventory)
         {
             item.RemoveAmount(item.GetQuantity());
             item.AddAmount(item.itemQuantity);
         }
     }
 
     private void Update()
     {
         currentItem = inventory[index];
 
         if (Input.GetKeyDown(KeyCode.Alpha1))
             index = 0;
 
         if (Input.GetKeyDown(KeyCode.Alpha2))
             index = 1;
 
         if (Input.GetKeyDown(KeyCode.Alpha3))
             index = 2;
 
         if (Input.GetKeyDown(KeyCode.Alpha4))
             index = 3;
 
         if (currentItem != null)
         {
             text.text = currentItem.gameObject.name;
             quantityText.text = currentItem.GetQuantity().ToString();
         }
         else
         {
             text.text = "None";
             quantityText.text = "n/a";
         }
 
 
         if (currentItem != null && currentItem.GetQuantity() != 0)
         {
             foreach(KeyCode key in keys)
             {
                 if (Input.GetKeyDown(key))
                 {
                     currentItem.ActionKeyDown(key, this);
                 }
 
                 if (Input.GetKey(key))
                 {
                     currentItem.ActionKey(key, this);
                 }
             }
 
             //if (Input.GetKeyDown(KeyCode.Q))
             //{
             //    DropItem(index);
             //}
         }  
     }
 
     public bool AddItem(Item item, int slot)
     {
         if (inventory[slot] == null)
         {
             inventory[slot] = item;
             return true;
         }
         else
         {
             return false;
         }
     }
 
     public bool AddItem(Item item)
     {
         if (currentItem == null)
         {
             currentItem = item;
             return true;
         }
         else
         {
             return false;
         }
     }
 
     public void RemoveItem(int slot)
     {
         inventory[slot] = null;
     }
 
     public void DropItem(int item)
     {
         if (inventory[item] != null)
         {
             cannonScript.Launch(GetComponent<playerReferences>().emitter, inventory[item].GetComponent<Rigidbody>(), 0);
             RemoveItem(item);
         }
     }


When the game starts, the inventory goes through all of the items in it and sets their quantity to 0. Then it adds their itemQuantity to their quantity. This is how I fixed it.

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 highpockets · May 10, 2019 at 06:32 AM 0
Share

This is not an answer. If you have solved your problem, state how you solved it.

avatar image edthered1009 highpockets · May 10, 2019 at 02:45 PM 0
Share

Is this better? I edited my previous post. Thank you for re$$anonymous$$ding me.

avatar image Bonfire-Boy edthered1009 · May 10, 2019 at 03:39 PM 0
Share

Not really. An explanation would involve stating why the original unwanted behaviour occurred and why what you've done now has fixed things. I can't see what you've changed that would make a difference.

Show more comments

Follow this Question

Answers Answers and Comments

107 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 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

Override method for items in xml list? 0 Answers

How to make an inventory for my text advenutre? 0 Answers

Inventory System: How to detect equal items on a list for increasing amount? 0 Answers

Best approach for inventory system with randomly generated items? 1 Answer

What's the name of this system? 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