- Home /
The question is answered, right answer was accepted
C# The call is ambiguous between the following methods or properties
I'm getting an error from the console saying "The call is ambiguous between the following methods or properties" and points me to this line of code. I'm not entirely sure what is wrong with this code since I've used vector3s with vector2s in other parts of my script with no issues. What exactly is wrong this code?
transform.Find("gameobject1").localPosition = transform.Find("gameobject2").localPosition + new Vector2(Random.Range(0.5f, 0.75f), 0.0f);
Answer by InGameDev · Dec 18, 2014 at 05:09 AM
use this transform.Find("gameobject1").localPosition = transform.Find("gameobject2").localPosition + new Vector3(Random.Range(0.5f, 0.75f), 0.0f,0);
You don't even need the "0" as the Vector3 constructor has an overload with only two parameters (x and y).
@DangerousBeans:
The problem here is that Vector2 has two implicit casting operators defined. So a Vector2 can implicitly converted into a Vector3 and a Vector3 into a Vector2 if necessary.
The ambiguity actually comes from the "+" operator. There is one for Vector2 that takes two Vector2 parameters and one for Vector3 that takes two Vector3 parameters.
Since you have one Vector3 and one Vector2 parameters the compiler has to look for an operator that works with those parameters. However, there is none. So it has to look for an implicit cast if it can cast one of the parameters into a type that one of the "+"operators can take.
The problem is that the Vector2 can be casted into a Vector3 and it will use the Vector3 "+"operator, but it's also possible to implicitly cast the Vector3 into a Vector2 and use the Vector2 operator. Both implicit casts are possible. Since the compiler has two ways to solve this issue it throws this error.
Follow this Question
Related Questions
C# GameObjectList not Setting Parent 0 Answers
C# Smoothing Out transform.Translate 4 Answers
C# Transform Issues 1 Answer
C# GUI.Button Transform.Position 1 Answer