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
2
Question by dakkuuan · Mar 09, 2014 at 02:37 AM · c#listarrays

Need help with converting an array to a list in C#`

Okay, I'm bringing in xml data, so I assume that has to be done with an array, or at least is done that way in this example: http://wiki.unity3d.com/index.php?title=Saving_and_Loading_Data:_XmlSerializer . Now since that part works, I don't want to mess around with it unless I have to, I need to convert deck information brought in from an array into a list, so it's easier to manipulate. Here is the bit of code that is given me an issue:

         var deck = DeckContainer.Load(Path.Combine(Application.dataPath, "kdeck.xml"));
         int test = deck.Dcards [0].Number;
         Debug.Log (test);
         int numofCards = deck.Dcards.Length;
         Dcard tempcard;
         Debug.Log (numofCards);
         for (int c = 0; c < numofCards; c++) {
             tempcard = deck.Dcards[c];
             playerdeck.Insert(c,tempcard);
             tempcard = playerdeck[c];
             Debug.Log (tempcard);
                 }

 please note that the playerdeck list is a Dcard list so that isn't the problem.  I keep the getting this error:  

`NullReferenceException: Object reference not set to an instance of an object now tempcard is a Dcard object, so I'm not quiet sure what's going on here. Note also you can't tell this from here, but deck.Dcards[] does hold Dcard objects. I can access the Dcard properties like this deck.Decards[0].Number and such, as defined in my Dcard class. Any ideas?

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
27

Answer by MakeCodeNow · Mar 09, 2014 at 04:17 AM

You can do either one of these:

 List<> someList = new List(someArray);

or

 someList.AddRange(someArray);
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
11

Answer by dakkuuan · Mar 10, 2014 at 09:18 PM

I found a solution as well, somelist = somearray.ToList (); with using System.Linq;

Comment
Add comment · 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
1

Answer by Master109 · Nov 05, 2018 at 05:45 PM

 using System.Collections.Generic;
 
 // namespace Extensions
 // {
     public static class CollectionExtensions
     {
         public static List<T> ToList<T> (this T[] array)
         {
             List<T> output = new List<T>();
             output.AddRange(array);
             return output;
         }
     }
 // }



Comment
Add comment · 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
0

Answer by solidearthvr · Sep 26, 2017 at 11:18 PM

Here's a good example:

 using System.Linq;
 using System.Collections.Generic;
 
 private void FindObjsMatchingPattern(string name) {
     List<GameObject> objs = GameObject.FindObjectsOfType<GameObject>().Where(obj => obj.name.Contains(name)).ToList();
     foreach (var go in objs)
     {
         Debug.LogFormat("found: {0}", go.name);
     }
 }
Comment
Add comment · 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

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

24 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 avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Query with C# arrays and List<> 2 Answers

A node in a childnode? 1 Answer

How to modify array values? 1 Answer

Distribute terrain in zones 3 Answers


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