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

五迷三道 提交于 2019-12-13 18:50:43

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!