I have a list of a custom class I made for in my game and when I click the object it is to add the quantity amount to that list entries quantity variable but instead it is adding on to it once and won’t do anything to it afterwards.
This is the quantity part of my custom class
public class InvItem : ScriptableObject {
public ItemType type = ItemType.Null;
public Item item = null;
public int quantity = 0;
public int Quantity {
get { return quantity; }
set { int temp = quantity + value;
quantity = temp; }
}
This is how the quantity is modified when told to add the item
public List inventory = new List();
public void AddItem(InvItem item) {
// i is only here for testing, testing for qauntity adding
int i = 0;
if (item.item != null item.quantity > 0) {
if (ContainsItem (item.item.id)) {
inventory[i].Quantity = 10;
} else {
if (inventory.Count < MAX_INV_SLOTS) {
inventory.Add (item);
}
}
}
}
This is how the item is Added when clicked on
private bool canInteract;
private InventoryManager invMan;
private InvItem newEntry = (InvItem)ScriptableObject.CreateInstance();
// Use this for initialization
public void Start () {
canInteract = false;
invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent();
}
// Update is called once per frame
void Update () {
if (canInteract) {
if (Input.GetButtonDown ("Interact")) {
newEntry.quantity = 1; // Will be randomly generated
invMan.AddItem(newEntry);
}
}
}
void OnMouseEnter() {
canInteract = true;
}
void OnMouseExit() {
canInteract = false;
}
public void SetItem(string type, Item item) {
newEntry.type = newEntry.SetType(type);
newEntry.item = item;
}
What am I doing wrong, I cannot figure it out?
first some pointers:
set { int temp = quantity + value;
quantity = temp; }
use +=
set { quantity += value; }
Also
public List inventory = new List();
personally I prefer generic lists, they’re speedier.
public List<InvItem> inventory = new List<InvItem>();
note it’s in the System.Collections.Generic namespace.
As for your main problem…
public void AddItem(InvItem item) {
// i is only here for testing, testing for qauntity adding
int i = 0;
if (item.item != null item.quantity > 0) {
if (ContainsItem (item.item.id)) {
inventory[i].Quantity = 10;
} else {
if (inventory.Count < MAX_INV_SLOTS) {
inventory.Add (item);
}
}
}
}
-
‘i’ is only ever set to zero. So if ContainsItem returns true, the first item is only ever modified… is this expected behavior?
-
Speaking of, what is ContainsItem? What does it return true for? You only ever add an item if ContainsItem returned false… so make sure it’s not returning true for reasons it shouldn’t. If it’s returning true always when there is something in the list, then ‘Add’ would only be called once.
-
Also when ContainsItem is called, this is probably when you should be getting the ‘i’ value… I’m assuming the “inventory*.Quantity = 10;” should be effecting the entry who matches the id in ‘ContainsItem’… unless I’m incorrect in understanding your intentions.*
4) Lastly you just set said qty to 10? That’s weird? Is this all like demo code or something cause that’s just strange behavior IMO. I’m not sure what the purpose of that is. This means your qty is ever only 1 or 10, never anything else.
I tried the += but it wasn’t working so I set that to a strict addition and setting the quantity to that variable so Im 100% positive it is getting the right value for testing im going to change it back afterwards.
I forgot the <> when I typed it into my code, the List is actually what I have in my code.
i is set to zero cause I only have one item to test, Im going to change that later to find the first of the same time of item and add to that quantity.
This is ContainsItem
private bool ContainsItem(int item) {
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].item.id == item.id) {
return true;
}
}
return false;
}
I will have i returned later as the first instance of the item, but for right now I know it is zero
I have the quantity hardcoded as ten (representing the item being added) so I can see a change in the quantity of the item in the inventory
My qauntity should start as one add 10 to that every time, it should just be 1 or 10. causing im sending the 10 into a +=
1+=10 >> 11+=10 >> 21+=10 >> 31
This is not the fnal code, I’m just trying to weed out this bug, cause its harder to do that when you introduce 50 more lines of code. lol
ContainsItem is taking in an ‘int’. int does not have a property ‘id’. How is that code even running?
Have you put in debug lines to make sure that the ‘else’ side where you say ‘inventory.Add’ is reached? If it’s not reached, something is wrong in ContainsItem, if it is reached… your List is messed up.
Most of the code you’re posting here is all jarbled and you needed to explain caveats as to why its like this. This makes the code confusing and bugs can creep in confusing code. Furthermore, how is the code even running if you left out critical bits of code like the on the list? It won’t compile otherwise. Are you hand writing this into the thread or something? If so even more transcription bugs could creep in.
Oh and a suggestion about the Quantity property where you add the value on in the setter. That’s smelly code. Most people wouldn’t suspect the property to act that way… and there’s nothing about the property that states it acts that way. Why not have it set, and if you want it to increment you increment it where you set it.
if I say:
obj.Quantity = 10;
I read that as saying:
“set the value of Quantity to 10”
NOT:
“increment the value of Quantity by 10”
Contains Item should be sending in a InvItem not an int
I did handwrite part of the code in, like the list in my code the list is set up correctly.
Ill repost all the code with explanations
In my OnLoad script which executes before everything else
// Instantiates the item
GameObject clone = (GameObject)Instantiate (prefab, newPos, Quaternion.identity);
//renames the item
clone.name = "Instantiated Cube";
// Gives it the ItemObject script
ItemObject iobj = clone.AddComponent<ItemObject>();
iobj.Start ();
// Sets up the item to be a InvItem
iobj.SetItem ("Weapon", iManager.itemList[0]);
When the item is interacted with, the quantity is set to 1 for testing purposes
void Update () {
// Set to true on mouse over
if (canInteract) {
if (Input.GetButtonDown ("Interact")) {
// Sets quantity to be added to one
newEntry.quantity = 1; // Will be randomly generated
Debug.Log ( newEntry.Quantity);
// Adds the item to the inventory
invMan.AddItem(newEntry);
}
}
}
This is the inventory manager. It is how the inventory list and adding is handled
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class InventoryManager : MonoBehaviour {
// Item = The item......int = the qauntity
public List<InvItem> inventory = new List<InvItem>();
private const int MAX_ITEM_STACK = 30;
private const int MAX_INV_SLOTS = 15;
public void AddItem(InvItem item) {
// i is only here for testing, testing for qauntity adding
// i is 0 so i will always get the first item cause i only have one item in the game so far
int i = 0;
// if The item being sent is is not null
if (item.item != null item.quantity > 0) {
// If the inventory already has the item
if (ContainsItem (item)) {
Debug.Log ("Item Q: " + item.quantity);
Debug.Log ("Inv Q: " + inventory[i].Quantity);
// Add to the Lists current quantity
inventory[i].Quantity += item.quantity;
Debug.Log ("Inv Q: " + inventory[i].Quantity);
} else {
// Else add the item to the inventory if we arent full on slots
if (inventory.Count < MAX_INV_SLOTS) {
// Add to the inventory
inventory.Add (item);
}
}
} else {
// Cant add to the inventory
Debug.LogError ("Can't add item to the inventory. Line 30.");
}
}
private bool ContainsItem(InvItem item) {
for (int i = 0; i < inventory.Count; i++) {
if (inventory[i].item.id == item.item.id) {
return true;
}
}
return false;
}
}
This is how my InvItem is setup
using UnityEngine;
using System.Collections;
[System.Serializable]
public class InvItem : ScriptableObject {
public ItemType type = ItemType.Null;
public Item item = null;
public int quantity = 0;
public int Quantity {
get { return quantity; }
set { quantity = value; }
}
public enum ItemType {
Weapon,
Armor,
Consumable,
Item,
Null
}
public ItemType SetType(string type) {
switch (type) {
case "Weapon":
return ItemType.Weapon;
default:
return ItemType.Null;
}
}
}
Alright, hopefully this clears up any confusion on the flow of adding the item
So I’m re-reading your initial post to see how this all comes together. And I’ve been thinking up to this point that it wasn’t adding a new entry to the list… but you’re problem isn’t that, it’s that when you get here:
if (ContainsItem (item)) {
Debug.Log ("Item Q: " + item.quantity);
Debug.Log ("Inv Q: " + inventory[i].Quantity);
// Add to the Lists current quantity
inventory[i].Quantity += item.quantity;
Debug.Log ("Inv Q: " + inventory[i].Quantity);
}
For an InvItem that already exists, that qty isn’t adding, but is instead ‘setting’.
OK… furthermore this code has changed before you posted it as:
if (ContainsItem (item.item.id)) {
inventory[i].Quantity = 10;
}
Note the difference between,
inventory*.Quantity = 10;*
and
inventory*.Quantity += 10;*
Now it also appears you’ve also changed the Quantity property itself removing the code smell I pointed out before. With all these changes from the original code you posted… does the error still actually occur?
I’m sorry, I’d love to help you, but everything you keep posting is just getting more obfuscated and muddled by contradictory statements and examples. I can’t make heads from tales of what is legite code and what isn’t. I barely even know what the question actually is anymore.
(This is why I pointed out that code smell can cause major problems, you might understand it, but it’s going to confuse the crap out of other people. It’s like handing leet speak to your English professor, they’re going to cringe and not bother reading it at all.)
I fixed the things you pointed out and reposted the code, the error still occurs. Everything before the post I just made can be ignored. The post I just did, was copy and pasted code straight from the file. I’m not sure how I am contradicting myself, or where you’re getting confused?
The question was, what is causing it to set the quantity to what I send in instead of adding to it.