问题
I am trying to do Vector2(-1, 0) instead of Vector2.left but it gives this error:
Non-invocable member 'Vector2' cannot be used like a method
Any ideas?
回答1:
Vector2.Left
is equal to new Vector2(-1,0)
; , not Vector2(-1, 0)
:)
回答2:
I think this happens because you use syntax like this:
Vector2 vec;
//assign new value
vec = Vector2(-1,0);
This is not gonna work because compiler thinks that you are using method called Vector2() which doesn't exist and it is not correct because you should create a new object and then assign its value to vec variable. For example:
Vector2 vec;
//assign new value
vec = new Vector2(-1,0); //you create a new Vector2 and assign its value to vec
or, the better way will be to store your Vector2(-1,0) in an individual variable. Like this:
Vector2 vec, leftVec;
leftVec = new Vector2(-1,0);
//assign new value
vec = leftVec;
This way you may change your varialble's value without creating a new object every time.
来源:https://stackoverflow.com/questions/32997907/error-non-invocable-member-vector2-cannot-be-used-like-a-method