- Home /
The question is answered, right answer was accepted
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?
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
$$anonymous$$y bet is that quantity, as a public field, is serialized as 0, and that's what you see.
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?)
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;
}
}
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.
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.
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.
This is not an answer. If you have solved your problem, state how you solved it.
Is this better? I edited my previous post. Thank you for re$$anonymous$$ding me.
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.
Follow this Question
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