Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by VrTechEx · Aug 13, 2013 at 09:08 AM · arraylist

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 :(

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image liszto · Aug 13, 2013 at 09:08 AM 2
Share

Can you post your error ?

avatar image vexe · Aug 13, 2013 at 09:19 AM 0
Share

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)

avatar image Jamora · Aug 13, 2013 at 09:22 AM 0
Share

vexe, you forgot the spaces after your angle brackets. He means public List Inventory = new List();

avatar image VrTechEx · Aug 13, 2013 at 09:52 AM 0
Share

Sorry I forgot to add errors :P hahaha Please help I update more information :)

avatar image vexe · Aug 13, 2013 at 04:16 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VrTechEx · Aug 14, 2013 at 04:09 AM 0
Share

this could make me understand the struct more better :)

avatar image
0

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];
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VrTechEx · Aug 13, 2013 at 10:01 AM 0
Share

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

avatar image vexe · Aug 13, 2013 at 10:05 AM 0
Share

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.

avatar image vexe · Aug 13, 2013 at 10:07 AM 0
Share

For more information about the error you're getting http://stackoverflow.com/questions/414981/directly-modifying-listt-elements

avatar image KiraSensei · Aug 13, 2013 at 12:07 PM 0
Share

Ah of course ... vexe told you a good correction for this now, and Joyrider gave you an example of implementation.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges