Whats the proper way to store an Item and show how many you have?
I was thinking about creating a List like so:
public List<Items> lItems= new List<Items>();
public List<int> lItemAmount= new List<int>();
Then when I want to add a item I just do:
private void AddToItems()
{
lItems.Add(Items);
lItemAmount.Add(Ranom.Range(1,10);
}
Is this a proper way or is there a better way of doing this?
list.Count
tells how many items in the list. I don't understand the purpose of the second line with the title. You add a random integer to the list when you add a new item? How's that related to "how many you have".
please reply as an Answer if you're answering a question. Then the author can mark it as the accepted answer and it will show as answered when navigating the pages on UnityAnswers.
It's common practice in UA to put partial "answers" plus requests for clarification in a comment. Like for this, the wording is odd enough they may be asking how to store "3 flashlights", "7 quills" in an inventory. Generally, "you weren't asking this, were you? If so..." is done as a comment here.
Sometimes the Q really was about basic C#, like List.count, but it's easy enough to change a comment to an answer.
Anyway, as an opinion question, which might be basic C# use, and seems like a duplicate (is it Inventory?) moving this to the HelpRoom area.
Answer by HenryStrattonFW · Nov 09, 2015 at 02:04 PM
As @fafase commented. You could use lItems.Count to return how many items are currently stored in the list as an integer. There is no need to have a secondary variable or storage container for this value (and definitely not for just adding a completely random number :P)
Follow this Question
Related Questions
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index 2 Answers
Problem when changing a value in the list 1 Answer
Accessing a class within a list 1 Answer
destroying instantiated prefab loaded to 2D list 0 Answers
reorderable list of reorderable list items? (Editor) 0 Answers