error CS0034: Operator '-' is ambiguous on operands of type 'Vector2' and 'Vector3' please help

this is my code error :
error CS0034: Operator ‘-’ is ambiguous on operands of type ‘Vector2’ and ‘Vector3’

This is not a 2D issue so isn’t appropriate for the 2D sub-forum. You should use the scripting forum here.

Quite simply you’re mixing the use of Vector2 and Vector3 and the C# compiler is giving you that error. You convert to Vector2 then subtract, presumably Vector3 from it. Just be careful with matching like for like when performing these operations.

Also, please note that you can use code-tags to post code, no need for screenshots.

can you tell me how to write the coorect one

actually when i saw a tutorial it worked for him on youtube

Didn’t I tell you above? I’d like to copy your code and paste it here but it’s a screenshot. Also, errors tell you the line and column too, not just a description.

(Vector2) converts the results from the ScreenPointToWorld, you then subtract a Vector3 from it. Don’t convert the result until you’ve subtracted the Vector3 from it.

Vector2 difference = Vector2.zero;
private void OnMouseDown()
{
difference = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
}

private void OnMouseDrag()
{
transform.position = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - difference;
}
here you go

difference = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition) - new Vector2(transform.position.x,transform.position.y);
1 Like

also deleting the
using System.Numerics;

works

It may work in whatever your ambigous compilation error was but that’s nothing to do with the problem in this thread which isn’t related to System.Numerics at all.

I can see you replied to this post here but the compiler telling you something is ambiguous isn’t solved by doing one very specific thing. Being ambiguous between UnityEngine.Vector2 and UnityEngineVector3 isn’t the same as being ambiguous between System.Numerics.Vector2 and UnityEngine.Vector2.

Compiler errors don’t always have a single root cause so stating a single specific solution isn’t valid.

Finally, please don’t necropost.

1 Like