错误:不可调用的成员';Vector2';不能像方法一样使用

本文关键字:方法 一样 Vector2 调用 错误 成员 不能 | 更新日期: 2023-09-27 18:16:42

我试图执行Vector2(-1,0(而不是Vector2.left,但它给出了以下错误:

Non-invocable member 'Vector2' cannot be used like a method

有什么想法吗?

错误:不可调用的成员';Vector2';不能像方法一样使用

Vector2.Left等于new Vector2(-1,0),不是Vector2(-1, 0):(

我认为发生这种情况是因为您使用了这样的语法:

Vector2 vec;
//assign new value
vec = Vector2(-1,0);

这是行不通的,因为编译器认为您使用的是不存在的名为Vector2((的方法,而且它是不正确的,因为您应该创建一个新对象,然后将其值分配给vec变量。例如:

Vector2 vec;
//assign new value
vec = new Vector2(-1,0); //you create a new Vector2 and assign its value to vec

或者,更好的方法是将Vector2(-1,0(存储在单个变量中。像这样:

Vector2 vec, leftVec;
leftVec = new Vector2(-1,0);
//assign new value
vec = leftVec;

通过这种方式,您可以在不每次创建新对象的情况下更改变量的值。