- Home /
How to assign List>Vector3>[]
Hi am assigning an array of lists
List<Vector3>[] paths = mp.vectorPaths; //where mp.vectorPaths is a List<Vector3> []
but I keep getting this error:
Cannot implicitly convert type UnityEngine.Vector3[][] to System.Collections.Generic.List<UnityEngine.Vector3>[]
What does this error mean
Have you made sure to include "using System.Collections.Generic" at the top of your file? Also is mp.vectorPaths also of type List? If it's not then you'll have to fill your List manually.
List
detonates a List
type variable, while T[]
detonates a built-in array type variable.
List[]
is a array of lists
Answer by benni05 · Mar 14, 2014 at 10:42 AM
Your mp.vectorPaths is not a
List<Vector3>
even though you might think it is. To prove this post the declaration of mp.vectorPaths here as well or double check. mp.vectorPaths is a 2-dimensional Vector3 array as the error message clearly says.
To make
<>
working, mark it as code sample.
I found out that mp.vectorpath is declared as
public Vector3[][] vectorPaths;
Is there any way I could assign it to my
List<Vector3>[] paths
Ok, yes, that's what I said. In general to make your Generic List work you would declare and fill it like this:
List<Vector3> paths = new List<Vector3>(); // empty now
paths.Add(new Vector3(1f,1f,1f)); // adding an example Vector3
You cannot assign a 2-dimensional array to your List just so. You could run in a nested loop over the 2 dimensions and fill your List with it. Also what are those 2 dimensions expressing? Guess you have to tell me why do you need the List, why is vectorPaths 2-dimensional and why would you like to put the contents of it into the List.
To make things nice, you could also use .ToList()
from Linq
Linq expressions can work but be careful though, they seem not to be supported on all platforms equally. See http://answers.unity3d.com/questions/376884/using-linq-when-building-to-ios.html
[1]: http://answers.unity3d.com/questions/376884/using-linq-when-building-to-ios.html
Linq works fine on iOS with .NET subset so long as you don't use Sum (although Aggregate works fine). There may be other edge cases with complex joins, but I've had no issues apart from that.
Answer by restush96 · May 11, 2021 at 05:25 PM
You can use this
public List<Vector3> vector3s = new List<Vector3>() { new Vector2(1, 0), new Vector3(2, 9), new Vector3(5, 7,10) };
Your answer
Follow this Question
Related Questions
Problem with Web GL Build 1 Answer
How to check if a value exists in an array (C#) 3 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Affect every object in array. 1 Answer