External Libraries in C# scripts

Just want to say first of all that I have a C/Objective-C programming background. I am new to Unity and C#.

That said, is there a way to #include or #import in unity? I cannot seem to figure out how to import classes from other files without assigning them to a game object (which seems a little complicated considering how easy unity makes other things). So, is there an import method, and how do I use it? If not how should I be using external code sources?

Hey,

There is no “#include” directive in c#. Any .cs files in your asset folder are accessible to all other scripts; you don’t need to attach them to a game object first. Note that unless a method in a class is declared “static” (like “alloc” in objective c), you will need to have an instance of that class before you can use it. For example:

public class TestClass 
{

  static void ThisMethodIsStatic()
  {
    // do stuff
  }

  void ThisMethodIsNonStatic()
  {
    // do other stuff
  }

}


// inside a different script.....
void Update()
{
  TestClass.ThisMethodIsStatic(); //okay

  TestClass.ThisMethodIsNonStatic();  // Won't compile, instead use the below

  TestClass instanceOfTestClass = new TestClass();
  instanceOfTestClass.ThisMethodIsNonStatic();
}

In addition, I believe that any .net assemblies in your “Assets” folder will also be available for use, but I haven’t tried this myself.

Does this help?

-Kevin