- Home /
vector3 is ambigous?
private Vector3 moveValues = Vector3.Zero;
im using the unity api example to write this
is it a problem I have with something in visual studio its between system numerics and unityengine
Answer by Bunny83 · Apr 17, 2020 at 01:28 AM
Well this is the reason why we actually have namespaces. So we can have types with the same name not conflicting each other. Keep in mind that you don't have to put any "using" statement at the top of your script. A using statement is just a shortcut for your whole file. So when you "use" a namespace you essentially bring it's members into your local scope and you can directly use the types / classes in that namespace without having to write out the namespace everytime you use the type in your file.
However if you import / use two namespaces which both contain a certain type the compiler has no way to determine which of the two types you wanted to use. So since System.Numerics has a Vector3 struct and the UnityEngine namespace has one as well you have a conflict here.
There are several ways to solve this issue:
First of all get rid of one of the using statements. Usually the one that contains less things you need. In Unity it wouldn't make much sense to get rid of
using UnityEngine;
since you will need a lot different things from that namespace. Now if you want to use something from the System.Numeric namespace you iust have to use the fully qualified type name. So instead ofXXXX
you useSystem.Numerics.XXXX
. That's what the compiler does under the hood anyways.The second solution is to still keep both using statements and add a specific using alias for the Vector3 type as well. A type alias looks like this
using Vector3 = UnityEngine.Vector3;
This will tell the compiler when faced with "Vector3" in your code it will use theUnityEngine.Vector3
type.Another solution would be to try to split your code into two seperate files where you can use different using statements. So the Unity related code will of course has it's using UnityEngine at the top and your other code that requires the System.Numerics namespace won't have the using UnityEngine statement. So this is more of an organisatorical approach.
Note that you get a similar issue when importing the UnitySngine
and the System
namespace. Both namespaces have a class called Random. If you import both you're in the same situation again. I would recommend to only using an alias if there's just one or max two conflicting classes. If there are more the other solutions are generally better. Many alias statements can make the code much harder to read.
Your answer
Follow this Question
Related Questions
Ambiguous Reference? 3 Answers
Ambigous Reference Animation 1 Answer
Ambiguous reference: how to fix ? -1 Answers
Ambiguous reference 'preview' 2 Answers