Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Gilead7 · Dec 11, 2017 at 08:36 PM · instantiateprefabarraylistwww

What is the best way to convert a string list to a class list?

I've retrieved WWW data and put it into a string array, removed the delimiter. Now I want to put it into a list an instantiate a prefab. I used linq to make it into a list. Strange thing is, there are three items taken from the database and the count reads as four.

         WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php"); 
         yield return VehicleData;
 
         Debug.Log (VehicleData.text);
         string TheVehicleData = VehicleData.text;
         string[] VehiclesArray = TheVehicleData.Split ('/');
 
         VehicleIndexList = VehiclesArray.ToList();
         Debug.Log (VehicleIndexList.Count);

The other issue is an error

  Cannot implicitly convert type `System.Collections.Generic.List<string>' to `System.Collections.Generic.List'

I've tried using cast, I've tried using as and the standard ()

 VehicleIndexList = (VehicleIndex)VehiclesArray.ToList() as VehicleIndex; 

This give the error Cannot convert type `System.Collections.Generic.List' to VehicleIndex'

 VehicleIndexList = VehiclesArray.Cast<VehicleIndex>().ToList(); 

This gives the error InvalidCastException: Cannot cast from source type to destination type. Which means the parameters aren't right

I've also tried using just the array, only I don't know how to instantiate a prefab from an array. I hope someone can help me out of this jam. Thanks!

Comment
Add comment · Show 1
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 ShadyProductions · Dec 11, 2017 at 09:01 PM 0
Share

What exactly are you trying to do? How do you want to convert a string to a class? That doesn't make much sense? Does the constructor of the class take in the string or something? Then you could do something like:

 var VehicleIndexList = VehiclesArray.Select(f => new VehicleIndex(f)).ToList();

Can you show us the VehicleIndex class?

1 Reply

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

Answer by fafase · Dec 11, 2017 at 09:08 PM

First you have one extra because your data most likely looks like:

  /data/data/data/

so it cuts it in 4.

You could check if the last is / and trim:

  if(data[data.Length - 1] == '/'){ data.TrimEnd('/'); }

Second you should show what type is VehicleIndexList. Sounds like you are not using a generic version of it.

   List<string>VehicleIndexList  = new List<string>( VehiclesArray);

But if you do not plan on changing the collection, then there is little point in doing so. If the purpose was to remove the last empty entry, using trimming from above will do it.

Comment
Add comment · Show 6 · 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 Gilead7 · Dec 12, 2017 at 12:18 AM 0
Share

The compiler doesn't like TrimEnd(). So after changing the array to a list, how do I convert that to a class list which I need to instantiate the prefab?

Found something that the compiler likes: VehiclesArray = VehiclesArray.Where (x => !string.IsNullOrEmpty (x)).ToArray ();

But it's still saying there are four in the length.

avatar image fafase Gilead7 · Dec 12, 2017 at 07:38 AM 0
Share

TrimEnd happens on the initial string, not on the split strings which is an array. You need to print the content so you know what your splits are.

But do you need the list? You have the collection from the split.

 string data= GetDataFromServer();
 if(data[data.Length - 1] == '/'){ data.TrimEnd('/'); } // Here parameter was missing...
 string[] results = data.Split('/', StringSplitOptions.RemoveEmptyEntries);
 foreach(string str in results){ Debug.Log(str); }

What is a class list? Do you mean non-generic ArrayList? You should not use those anymore. They are from the dawn of C# days, only legacy code makes use of those.

avatar image Gilead7 fafase · Dec 12, 2017 at 05:10 PM 0
Share

It spit out two errors: The best overloaded method match for string.Split(params char[])' has some invalid arguments and cannot convert System.StringSplitOptions' expression to type `char'

Any thoughts?

Show more comments
avatar image Gilead7 · Dec 12, 2017 at 04:27 PM 0
Share

The class list has ID, $$anonymous$$ake, $$anonymous$$odel, Year. I want to instantiate the prefab to display those elements.

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

115 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 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 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 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 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

instantiate problem help? 1 Answer

How to put gameObjects to the list? 4 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Parenting an instantiated prefab. 1 Answer

Array of prefabs 0 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