Search Unity

I'm getting CS0246 (type or namespace not found) even though the script in question exists

Discussion in 'Scripting' started by Jimmy-P, Jul 23, 2019.

  1. Jimmy-P

    Jimmy-P

    Joined:
    Jul 10, 2014
    Posts:
    59
    I'm trying to call a method on an object, passing a list of Person objects as an argument. The Person class is my own creation.

    I'm getting this error:
    Assets\Plugins\SQLiter\Scripts\SQLite.cs(289,34): error CS0246: The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?)

    This is the line in question:
    Code (CSharp):
    1. public void InsertPersons(List<Person> personList)
    The Person script is located under Assets\Scripts\Person

    I've tried wrapping the Person class in a namespace "PersonSpace" and added using PersonSpace; at the top in SQLite.cs but I get the same error (twice instead of just on line 289)

    Really at a loss here, as far as I can tell, as long as they are in the same project in the Assets folder somewhere, they should be able to find each other. Would greatly appreciate any tips or direction.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    It's about compilation order. Unity compiles certain folders (including Plugins) before the rest of the scripts in the project, so scripts inside Plugins can't know about anything outside of Plugins.

    You can move Person.cs inside a Plugins folder, or create a BasePerson.cs within Plugins that Person derives from (and use BasePerson from SQLite), or you can restructure your code so as to not need to reference Person with Plugins. The latter is probably the hardest but generally the most recommended option - it will allow you to, for example, update your install of SQLite in the future without wrecking changes you've made to it.
     
    Jimmy-P likes this.
  3. Jimmy-P

    Jimmy-P

    Joined:
    Jul 10, 2014
    Posts:
    59
    Thank you, I did just now try to move Person.cs into the plugins folder and that worked. No problems for the other scripts I have in assets\scripts to access it, so I suppose that is an OK solution for now.

    I had considered restructuring the code, but I can't really think of an alternate way to achieve what I want. I think I might do as you suggested and create a BasePerson to start with and revisit the whole thing later on. Thanks a lot.