Cannot implicitly convert type `System.Collections.Generic.List' to `UnityEngine.Vector3[]'
Hey!
so i got this
public List<Vector3> paths;
and want to fill its content into a
public Vector3[] points;
like this:
gameObject.GetComponentInChildren<DrawSpline>().points = paths;
but that leads to this error:
error CS0029: Cannot implicitly convert type `System.Collections.Generic.List<UnityEngine.Vector3>' to `UnityEngine.Vector3[]'
i assume i can not just copy over from a over to a [] array(?) but what but be the right way to do that?
To go back and forth between Array and List, there are default functions that can help (although doing it manually is not much effort).
// Array to List<Vector3>
points = paths.ToArray ();
// Adding all contents from your Array to the List<Vector3>
paths.AddRange (points);
// Alternatively, if you only want ONLY the array values
paths.Clear ();
paths.AddRange (points);
thanks! ToArray() helped! you should have post that as answer, so i can give you an accept :)
Answer by roman_sedition · Nov 12, 2016 at 01:40 PM
You aren't specifying which element of the array you are assigning, maybe use a for loop for this if you want to completely copy over
e.g
for(int i = 0, i < paths.count; i++)
{
gameObject.GetComponentInChildren<DrawSpline>().points[i] = paths[i];
}
Your answer
Follow this Question
Related Questions
How do i move a list object at the end of the list while shifting back the others? 1 Answer
Unknown Argument Out of Range Index Error On Card Game 1 Answer
How do I add 10 UI text elements to an array or a list...? 1 Answer
Which one to use for, while or something else? 1 Answer
Add an Object to a List which is part of an array does not work. 1 Answer