Question by
SimuStudios · Jan 18, 2018 at 04:26 AM ·
scripting problembug
Int not increasing in correct amounts
Hey so this isn't a scripting problem as much as it's just a little bug (or at least I think it's a bug) that I'm trying to avoid. Basically I'm using OnCollisionEnter when the player collides with an item, then in the inventory that item's stock should increase by 1. The problem I'm facing is that sometimes the amount will increase by 2 or sometimes even 3. Any advice on how to avoid this is more than appreciated!
Thanks for your help.
Comment
Here is the piece of code in question.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Item")
{
if (!itemName.Contains(collision.gameObject.name))
{
itemName.Add(collision.gameObject.name);
itemAmount.Add(1);
item.Add(collision.gameObject);
}
}
if (collision.gameObject.tag == "Item")
{
if (itemName.Contains(collision.gameObject.name))
{
itemAmount[itemName.FindIndex(x => x==collision.gameObject.name)]++;
}