- Home /
Question by
kelvin-w · Dec 03, 2017 at 02:34 PM ·
c#listserializable
adding items to my list
i have a script which i use as a list which looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class CollectedItems{
public string itemName;
public int itemCount;
}
i want to add new items to it by giving the name and the amout of it. but i cant get it to work.
how can i add a new item which contains a string and an int?
Comment
Best Answer
Answer by ShadyProductions · Dec 03, 2017 at 02:38 PM
public class Inventory
{
// Make list of the collected items class
public List<CollectedItem> Items = new List<CollectedItem>();
// Method to add an item
public void AddItem(CollectedItem item) {
Items.Add(item);
}
}
public class CollectedItem
{
public string itemName;
public int itemCount;
}
Something like this perhaps?
This is basic C# knowledge...
var item = new CollectedItem();
item.itemName = "Excalibur";
item.itemCount = 1;
AddItem(item);
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to make a dynamic list of a serializable variable? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How Do I Access and Change Items in a List on Another Script? 2 Answers