- Home /
Cannot convert object[] to Vector3[]
I have the following situation. The method I am using works fine with singular values, but it does not work for (any) arrays. My question is how do I get this to work for arrays? Please see attached screenshot that verifies the type is congruent with the type that the field is looking for.
The full error: ArgumentException: Object type System.Object[] cannot be converted to target type: UnityEngine.Vector3[] Parameter name: val
For completeness, "info" is of type FieldInfo
basically, you need to typecast the array (like by cycling through each element and copying individually).. the Vector3 is being stored as an "object". if you post the code, I can help explain further...
Answer by Doeko · Jul 30, 2013 at 02:25 PM
I have found the following related problem and answer: http://stackoverflow.com/questions/13790527/c-sharp-fieldinfo-setvalue-with-an-array-parameter-and-arbitrary-element-type
Answer by hoy_smallfry · Jul 30, 2013 at 07:10 PM
you can probably use the Linq extension method Select
to help with this:
// up at the top with others
using System.Linq;
//code in-between...
var vector3Array = o.Select(element => element as Vector3).ToArray();
info.SetValue(component, vector3Array);