- Home /
class List adding to variables are assigning not adding
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?
Format your code by indenting it by 4 spaces, or 1 tab before posting, or by selecting it and clicking on the 101 button. I've done it for you this time.
Is your inventory list a
List<InventoryItem>
and that detail was lost with the code formatting issues?
There are some syntax errors in your code. Please check if the above is the exact code in your script.
Answer by ryba · Jan 02, 2013 at 12:15 PM
Your description of problem is rather hard to understand, but ill try to help anyway. First of all, do your code compiles ? I doubt it. If your code has compile errors, first eliminate them, then ask questions.
What you should change at first in my opinion (first line is your code, second line is what it should look like) :
public List inventory = new List();
public List<InvItem> inventory = new List<InvItem>();
using System.Collections;
using System.Collections.Generic;
invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent();
invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent<InventoryManager>();
private InvItem newEntry = (InvItem)ScriptableObject.CreateInstance();
private InvItem newEntry = ScriptableObject.CreateInstance<InvItem>();
Method SetItem(string type, Item item) seems to be bad at all. This method doesnt change your newEntry to another item instance, you change only current instance fields. It would be better if it looks like that:
public void SetItem(string type, Item item) {
newEntry = ScriptableObject.CreateInstance<InvItem>();
newEntry.type = newEntry.SetType(type); // This row looks comic - but i cannot say anything else because i dont know how looks method SetType implementation
newEntry.item = item;
}
After all, method AddItem(InvItem item) inside manager also looks wrong. It doesnt actually add anything anywhere. Solution would look like that:
public void AddItem(InvItem item) {
if (item.item == null || item.quantity <= 0) {
// parameter validation - use way with return to avoid unnecessary indents
return; // if item is not valid, get out of this method and dont change anything
}
if (ContainsItem (item.item.id)) { // I fear this method is bugged, as most of your code :(
i = 0;
inventory[i].quantity += item.quantity; // You must get somehow index for your item, maybe its should be returned from ContainsItem ?
} else {
if (inventory.Count < MAX_INV_SLOTS) {
inventory.Add (item);
}
}
}
Thats good that you trying, learning, but dont expect you can really manage to finish game with your current skills, no offence - but you are making most trivial mistakes.