- Home /
Problem with List and Array?
I changed my code a bit... (2nd edited)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Stats : MonoBehaviour {
public static Stats Instance;
public List<ItemList> Inventory;
public string[] ItemNames;
public int[] ItemCounts;
void Awake () {
Instance = this;
Inventory = new List<ItemList>[ItemNames.Length];
for(int i = 0; i < ItemNames.Length + 1;i++)
{
Inventory[i].Name = ItemNames[i];
Inventory[i].Count = ItemCounts[i];
}
}
This is what my ItemList looks like !
using UnityEngine;
using System.Collections;
public struct ItemList
{
public string ItemName;
public int ItemCount;
}
The error is
Stats.cs(16,9): error CS0029: Cannot implicitly convert type `System.Collections.Generic.List<ItemList>[]' to `System.Collections.Generic.List<ItemList>'
Stats.cs(20,23): error CS1061: Type `ItemList' does not contain a definition for `Name' and no extension method `Name' of type `ItemList' could be found (are you missing a using directive or an assembly reference?)
Stats.cs(21,23): error CS1061: Type `ItemList' does not contain a definition for `Count' and no extension method `Count' of type `ItemList' could be found (are you missing a using directive or an assembly reference?)
How Can I use List with Array?
Same type?
string = string
int = int
Any Ideas??
I try to do a little Inventory with stackable items :(
There is nothing wrong with your code, or using arrays with lists. You are using your list without initializing it (as far as I can see from your code) you should do public List &ItemList& Inventory = new List &ItemList&(); (I can't use less/bigger than signs in comment)
vexe, you forgot the spaces after your angle brackets. He means public List Inventory = new List();
Sorry I forgot to add errors :P hahaha Please help I update more information :)
The second and third errors are easy to fix, you are using "Count" and "Name", but yet you have them as "ItemName" and "ItemCount" in your struct.
Answer by Joyrider · Aug 13, 2013 at 10:03 AM
You can't directly access the mehods of a list entry when using a struct type... create a temp variable and add that one to your list.
...
Inventory = new List<ItemList>();
tempItemList = ItemList();
for(int i = 0; i < ItemNames.Length;i++)
{
tempItemList.ItemName = ItemNames[i];
tempItemList.ItemCount = ItemCounts[i];
Inventory.Add(tempItemList);
}
}
Answer by KiraSensei · Aug 13, 2013 at 09:57 AM
Try :
Inventory = new List<ItemList>(ItemNames.Length);
for(int i = 0; i < ItemNames.Length + 1;i++)
{
Inventory[i].ItemName = ItemNames[i];
Inventory[i].ItemCount = ItemCounts[i];
}
}
I tried this and it solve the first error :)
then the new one comes :(
Stats.cs(20,10): error CS1612: Cannot modify a value type return value of System.Collections.Generic.List.this[int]'. Consider storing the value in a temporary variable Stats.cs(21,10): error CS1612: Cannot modify a value type return value of
System.Collections.Generic.List.this[int]'. Consider storing the value in a temporary variable
I would do this: Inventory = new List(); for(int i = 0; i < ItemNames.Length + 1;i++) { Inventory.Add(new ItemList(ItemNames[i], ItemCounts[i]); }
$$anonymous$$ake a constructor for your ItemList that accepts a string and an int.
For more information about the error you're getting http://stackoverflow.com/questions/414981/directly-modifying-listt-elements
Ah of course ... vexe told you a good correction for this now, and Joyrider gave you an example of implementation.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Creating a record of values generated with Random.Range 1 Answer
Need help with switch case in order/timed 0 Answers
Create a button from an int, remove button when clicked, and never load it again 1 Answer
Make the audio manager select from an array of sounds 1 Answer