Search Unity

Access to custom class. Not found. Namespace issue?

Discussion in 'Getting Started' started by Sparticus, Oct 28, 2020.

  1. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    Hey all,

    I have a custom class that looks like this :

    Code (CSharp):
    1. public class GridTile {
    2.     public int type;
    3.     public int row;
    4.     public int col;
    5.  
    6.     public GridTile(int type, int row, int col) {
    7.         this.type = type;
    8.         this.row = row;
    9.         this.col = col;
    10.     }
    11. }
    This class is located in the same physical directory as my MapManager.cs class. My MapManger would like to create an instance of this class but I get the following error :

    upload_2020-10-28_10-38-40.png

    I assume this is a namespace issue. I have tried adding the following to the top of my MapManger class :

    Code (CSharp):
    1. using GridTile;
    But that didn't help. How do I get this to work?

    Thanks!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    using only works with namespaces. Is there a namespace directive anywhere in either of those files?
     
  3. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    No, I don't have a name space in either file. I did try adding a namespace to just the GridTile class (not the class I am trying to use it in however)....

    I did the following :
    Code (CSharp):
    1. namespace SampleNamespace {
    2.  
    3. public class GridTile {
    4.     public int type;
    5.     public int row;
    6.     public int col;
    7.  
    8.     public GridTile(int type, int row, int col) {
    9.         this.type = type;
    10.         this.row = row;
    11.         this.col = col;
    12.     }
    13. }
    14. }
    And in my MapManger class I did :

    Code (CSharp):
    1. using SampleNamespace.GridTile;
    Which didn't appear to work...



     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    That would be just
    using SampleNamespace
    (you identify the namespace, not the name of some class within the namespace).

    But yeah, just take that stuff out entirely. This is the time to simplify, not complexify. :)

    Does this error actually appear in Unity? Or is it a Visual Studio problem?
     
    Joe-Censored likes this.
  5. Sparticus

    Sparticus

    Joined:
    Mar 15, 2014
    Posts:
    149
    VSCode is complaining it doesn't know what that class is. I haven't actually tried running the code while that VSCode error was there.

    What do you mean by take that stuff out completely? I never wanted namespaces to begin with.... but putting both classes in the same directory and just trying to do :

    Code (CSharp):
    1. GridTile tile = new GridTile(gridType, x, y);
    Didn't work as it VSCode couldn't find GridTile.... which is why I assumed I needed to set up some sort of namespaces...
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    VSCode sometimes gets confused. Ignore it and see what error (if any) you get in Unity.