Search Unity

Not able to automatically import my custom class?

Discussion in 'Scripting' started by ram23, Apr 30, 2017.

  1. ram23

    ram23

    Joined:
    Nov 5, 2015
    Posts:
    34
    Hey guys,

    I ran into a pretty annoying problem that I'm not able to solve, here's the issue...

    First I had the auto generated class that unity makes when I make a new script inside unity (A script attached to my camera)

    Second I made a different script called it "CustomClass" that was generated automatically from unity as well. Inside that script I deleted everything and simply made a class that looks like this --->

    public class CustomClass
    {
    public int health = 10;
    public int maxHealth = 10;
    }

    I went back to my other class the one that was auto-generated and wanted to us the public class I had just made. I thought it would automatically be detected without me having to manually import it. Sadly this was not the case I ended up having to go into visual studio right clicking the assets folder, clicking on existing item then adding the CustomClass.

    Any reason why this wasn't automatically added to my already existing class when I wanted to use it? Is there anything I can do in the future to change this without having to manually import new classes I make every time I want to use them?

    Thank you,
    Ram
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm not sure what you are asking. Any script saved in the assets folder will be imported automatically...
     
  3. ram23

    ram23

    Joined:
    Nov 5, 2015
    Posts:
    34
    Lets say I have script A and script B and lets say script B is custom code which only has the class I made no start() or update() methods in it.

    If I have script A and want to use the code in script B I'm only able to use the script B class inside the script A class if I manually import it by adding (as I described above). I cant use it in script A automatically even though both scripts are in the assets folder.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm still not getting it. Care to show example code?
     
  5. ram23

    ram23

    Joined:
    Nov 5, 2015
    Posts:
    34
    [/IMG] 1.jpg 2.jpg 3.jpg See how I had to add the class?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Weird. I've never seen that behaviour before.
     
  7. ram23

    ram23

    Joined:
    Nov 5, 2015
    Posts:
    34
    Solved- make sure to reload all when making a new class
     
    mirzasty likes this.
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    So Unity doesn't just create a script when you click "add script". It also adds the script to the meta-information for the correct C# project.

    C# projects are a bit unlike projects in other languages in that having stuff in folders under the project isn't enough for the project to care about them. It also has to be listed in the .csproj file. Or, at least that's how the common build processes work? I'm not actually sure about the standard.

    Anyways, so if you add the classes "Test" and "Test2" to your Unity project, it will automatically change the .csproj file so it has a part looking like this:

    Code (csharp):
    1.  
    2.   <ItemGroup>
    3.      <Compile Include="Assets\Test.cs" />
    4.      <Compile Include="Assets\Test2.cs" />
    5.  
    But, if you already have Visual Studio open, and don't press "reload all", it won't reload the .csproj file, and VS won't notice that the files are there. The right-click/add item thing you were doing was essentially just adding those things to the project manually.

    This is how working with C# in any framework works, the only thing in Unity is that you're creating scripts outside of VS.

    So, when working with Visual Studio or any other IDE, be sure to set it to automatically reload all when it detects changes. Or just create the scripts directly from within VS.