- Home /
Error adding Vector3 to Object list
Hi all. So here is the code in C# and i get errors when trying to add Vector3 direction to list:
List<Object> arr;
void DrawRoute(Vector3 direction) {
if(step == 0) {
tempPos = tempPos + direction;
arr.Add(direction);
step++;
}
else {
tempPos = tempPos + direction;
arr.Add(direction);
step++;
}
}
It works perfectly in JS, but somehow none of the methods from google worked in c# and i can't even compile it, because it throws errors. If i change list type to Vector3 it works, but i need to store different type of variables in it and it works in JS with "Object" type, so any help would be appreciated. I get following errors:
CS1503: Argument '#1' cannot convert 'UnityEngine.Vector3' expression to type 'UnityEngine.Object'
and
CS1502: The best overloaded method match for 'System.Collections.Generic .List.Add(UnityEngine.Object)' has some invalid arguments
Answer by fafase · Apr 14, 2014 at 09:05 AM
Try with object lower o on the front.
Vector3 is a struct so it does not inherits from Object but it should inherits from object which the .NET implementation and is the top class of all, even UnityEngine.Object.
Don't know why i didn't thought about it before...Thanks a lot !
Though this kind of go against what C# is all about, typesafe program$$anonymous$$g. You use a list of objects, probably you are storing other type of objects, well personal point of view, I don't recommended it, you better have a list for each type. But this is all personal.
I will keep that in $$anonymous$$d, thanks. Just all of my scripts are based on this list mainly and I am just converting them from JS to C#.