✅ Quick - Unity "using" directive
Really quick question guys, when trying to use methods from other scripts in Unity, let's say the other script's name (and therefore the other script's class name) is
Decoders, do I do using Decoders; or using static Decoders;?3 Replies
C# is C#
using is for namespaces
And lets you use all classes from that namespace
using static is for static classes and lets you use methods of that class directlyOk perfect thank you
Because Unity flashed me an error for doing
using static but it disappeared when I changed it to using - and now the opposite behaviour is happening when I try to reproduce it - must be a glitch of some sorts.when
using static is used against a type,
it means to import all static members of said type (the type itself is excluded, unless it's been imported by another using).
static members include:
- nested types
- fields with the static modifier
- properties with the static modifier
- methods with the static modifier
- fields with the const modifier
it's important to note that the type itself doesn't have to be static.
additionally, the use of the using keyword in this syntax:
oror
is meant to introduce an alias for a given namespace and/or type.
the following however:is used to import a single type rather than define an alias for it,
to exclude all other types in said Namespace that might cause ambiguity or some other issues...