Checking if a Vector2 is equal to something in unity

I'm trying to get the data of an object in 2d. It's saying I cannot check the position of an object, but I really need to check it. Here is my code:

void Update() < var Plr = GameObject.Find("Game").transform.Find("Image").GetComponent(); var pos = Plr.localPosition; if (Input.GetKey("w")) < if (Plr.localPosition = Vector2(pos.x, 0))< Plr.localPosition = new Vector2(pos.x, 320); >else < Plr.localPosition = new Vector2(pos.x, 0); >> if (Input.GetKey("s")) < if (Plr.localPosition = Vector2(pos.x, 0))< Plr.localPosition = new Vector2(pos.x,-320); >else < Plr.localPosition = new Vector2(pos.x,0); >> > > 
The error I'm getting:
Assets\playerS.cs(29,38): error CS1955: Non-invocable member 'Vector2' cannot be used like a method.
89k 9 9 gold badges 87 87 silver badges 132 132 bronze badges asked May 22, 2022 at 23:47 13 6 6 bronze badges

== for equality, = for assignment. Adjust your if statements. Also need new before the Vector2 in the if statements

Commented May 23, 2022 at 0:09

@Matt Assets\playerS.cs(29,17): error CS0034: Operator '==' is ambiguous on operands of type 'Vector3' and 'Vector2'.

Commented May 23, 2022 at 1:34

It would appear that you are not comparing two Vector2 s against each other. Please update your question to highlight what data types you are comparing and what you are trying to achieve.

Commented May 28, 2022 at 12:40

Please use the correct tags! Note that [unityscript] is or better was a custom JavaScript flavor-like language used in early Unity versions and is long deprecated by now.

Commented Jun 1, 2022 at 9:35

1 Answer 1

You're using this check several times:

Plr.localPosition = Vector2(pos.x, 0)) 

which is wrong for one because you need the double equals to test for equality (one equals is the assignment operator) and for two because you can't do just Vector2(pos. x, 0) . You have to make a new Vector2 to pass arguments. Not including the new keyword there is why you're getting the error you cite in the question.

In general though you've got these hard-coded magic numbers 0 and 320. When you use values like that and want to change them later it's easy to replace all but one and then get bugs. If you're using constants, just make them constants and refer to them by name. If you give it a descriptive name, like laneOffset or laneCenter then it becomes much clearer what you're doing.

Also you're making a lot of new vectors. They're structs so it's not the end of the world but it's a terrible habit to get into. Try to think critically about what you're doing and always try to avoid new-ing if you can. In your case all you seem to really care about is the y position, so just check that! Consider the following and think about readability when you review it:

private const float laneOffset = 320f; private const float laneCenter = 0; void Update() < var Plr = GameObject.Find("Game").transform.Find("Image").GetComponent(); var pos = Plr.localPosition; if (Input.GetKey("w")) < if (pos.y == laneCenter)< pos.y = laneOffset; >else < pos.y = laneCenter; >> if (Input.GetKey("s")) < if (pos.y == laneCenter)< pos.y = (-laneOffset); >else < pos.y = laneCenter; >> Plr.localPosition = pos; > 

I use parentheses around the negative lane offset in the "s" case as a visual cue that it's supposed to be different - bankers and finance types do the same thing for the same reason. No more new, no more magic numbers. Hopefully you see what I mean about readability :-)