Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is there a way to #include "nameOfScriptFile.cs" in another script file

Discussion in 'General Discussion' started by lkarus, Mar 7, 2015.

  1. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    So I come from C++ and I was wondering if it is possible to include the 1 cs file in another cs file.
    Currently I have them all in 1 cs file and its getting quite out of hand.

    So for example I would like to have
    //baseScriptfile.cs

    public class baseClass
    {
    }

    //childClassFile.cs

    public class childClass : public baseclass.
     
  2. Jaimi

    Jaimi

    Joined:
    Jan 10, 2009
    Posts:
    6,169
    Yes. But try to follow standard norms.

    Each class should have it's own file. That includes base classes, etc. They will all magically find each other as long as they're all in the same namespace.

    You can break a single class into multiple files by creating partial classes. This is normally used when you have some process generating parts of the class, and you want to make hand edits to it as well.
     
  3. knr_

    knr_

    Joined:
    Nov 17, 2012
    Posts:
    258
    Hi Teekay,

    What Jaimi said. In C# there are no #include statements. In C#, if you reference a C# class that exists in a different file, as long as the file is included in the project and is in the same namespace, you can reference it already without having to do anything more.

    Welcome to the world of automatic garbage collection!
     
  4. lkarus

    lkarus

    Joined:
    Feb 25, 2015
    Posts:
    51
    Thanks for the reply guy!!
    And thank you for your help :D
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The bigger trick in c# is keeping classes that shouldn't know about each other seperate.

    There is no equivalent of a friend class, that would be nice from time to time.
     
  6. Zerot

    Zerot

    Joined:
    Jul 13, 2011
    Posts:
    135
    Yeah. There is some separation possible at the assembly level(e.g. internal), but outside of that there is no way to have friend classes.