Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

Help Wanted I can't access my namespace from another C# namespace

Discussion in 'Scripting' started by tanmaykulkarni, May 6, 2021.

  1. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    42
    Heyy, I am trying to access a script from one namespace from an another. So here what's I am doing.

    Here's a variable of Script X in Namespace A

    Code (CSharp):
    1. namespaceA
    2. {
    3.        public class ScriptX
    4. {
    5.        public int VarA;
    6. }
    7. }

    And from this class I want to access VarA. Here's my another script in which I am trying to access this variable

    Code (CSharp):
    1. namespace NamespaceB
    2. {
    3.       public class ScriptY
    4. {
    5.      //How to access that variable?
    6.    
    7. }
    8. }
    I tried putting using NamespaceA; in the top of ScriptY but it gives an error "Type of namespace doesn't exists". So how do I access that variable??
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    15,584
    Your problem:

    namespaceA
    is NOT the same thing as
    NamespaceA


    100% of all characters must be perfect: spelling, capitalization, punctuation and spacing (or not).
     
    Joe-Censored likes this.
  3. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    42
    Yeah, I just gave example, in my script I tried using everything exact in my scripts
     
  4. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,071
    Post the Actual scripts then

    and if you're going to write example code in the future, use a beautifier to format it properly so that it is readable
    https://codebeautify.org/csharpviewer
     
    Last edited: May 8, 2021
    Bunny83 likes this.
  5. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    6,853
    Please don't. Use the CODE tags in the forum editor toolbar. It was created for this.
    screenshot1.png

    The "code beautify" (and proceeds to put {} on same line...) changes a lot of things in the code, you will lose your connection with the original. I mean the 30th line in a code won't be the 30th line anymore in reality if you put through this. Do not do it.
     
    rubcc95 and Bunny83 like this.
  6. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,071
    If you are pasting actual code from VS, it is already formatted. If you are just typing out example code without touching VS, the code tags won't format it for you. He did use code tags, and he typed his example code out, and that is the result
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    6,853
    Code (CSharp):
    1.         protected override void Save(params bool[] values) {foreach (var value in values) _writer.Write(value);}
    2.         protected override void Save(params byte[] values) {foreach (var value in values) _writer.Write(value);}
    3.         protected override void Save(params sbyte[] values) {foreach (var value in values) _writer.Write(value);}
    4.  
    Copy from Rider, pasted as plain text (same line code is me, not the auto format).

    That's the thing: do not type in your code, unless you know what you're doing. If you aren't sure, CTRL+A in your script file, hit the CODE icon in the forum post editor toolbar, in the popup window CTRL+V. Submit. That's all you need to do to get properly formatted code.
     
    BenniKo likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    15,584
    Well, that's as may be, but we already know that this is NOT valid C# code:

    Code (csharp):
    1. namespaceA  // not valid; copied from your first post above
    2. {
    3. }
    You certainly did not type that 100% from functioning code because... it's NOT valid C# syntax.

    We know this because you ALSO have:

    Code (csharp):
    1. namespace NamespaceB   // possibly valid, copied from your first post above
    2. {
    3. }
    which could be considered valid C#.

    Perhaps you meant:

    Code (csharp):
    1. namespace NamespaceA
    2. {
    3. }
    for the first block of code?

    You may want to consider a brief refresher in C# namespacing and scoping and general language syntax so these sorts of simple typos instantly leap out to you and you recognize what you have omitted or mistyped.

    You see, the 100% requirement really is 100%, not 99.99%.
     
  9. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    42
    Yeah, I know that, I typed wrong in hurry. My code is absolutely right. And what I get as error is the type or namespace doesn't exists while it is there
     
  10. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    104
    its incredible everybody is jumping on that capital letter. this is one way to do it

    Code (CSharp):
    1. namespaceA
    2. {
    3.        public class ScriptX
    4.        {
    5.              public int VarA;
    6.        }
    7. }
    8.  
    9. namespace NamespaceB
    10. {
    11.       public class ScriptY
    12.      {
    13.            //How to access that variable?
    14.            NameSpaceA.ScriptX.VarA
    15.      }
    16. }
     
  11. BaBiAGameStudio

    BaBiAGameStudio

    Joined:
    Mar 31, 2020
    Posts:
    98
    Then, as everyone is already saying, post your code.
    If you don't, then people can't help you as we will all be guessing at what you have done, especially as your examples were invalid.
     
  12. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    6,853
    Standard C#.
    Code (CSharp):
    1. using System;
    2.  
    3. namespace A
    4. {
    5.     public class ScriptX
    6.     {
    7.         public int varA = 666;
    8.     }
    9. }
    10. namespace B
    11. {
    12.     public class ScriptY
    13.     {
    14.         private void Something()
    15.         {
    16.             var scriptX = new A.ScriptX();
    17.             Console.WriteLine(scriptX.varA);
    18.         }
    19.     }
    20. }
    21.  
    or if it's
    static
    Code (CSharp):
    1. using System;
    2.  
    3. namespace A
    4. {
    5.     public class ScriptX
    6.     {
    7.         public static int varA = 666;
    8.     }
    9. }
    10. namespace B
    11. {
    12.     public class ScriptY
    13.     {
    14.         private void Something()
    15.         {
    16.             Console.WriteLine(A.ScriptX.varA);
    17.         }
    18.     }
    19. }
    20.  
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,841
    Your code is absolutely right and yet you're getting a compile error?

    One of those two things is false.
     
  14. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    42
    I tried this only, it is giving error
     
  15. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    5,841
    You still haven't shared your code. Without seeing your code everyone here is just taking shots in the dark.

    Heck you haven't even shared the actual error message you're getting.
     
  16. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    6,853
    I think it's a troll. He had three days to come up with some more clear explanation what is it he wants to do...
    And avoiding every sane message. Unless he already put us on ignore list for some reason, but in that case, he's a troll.
     
    Last edited: May 9, 2021
    Kurt-Dekker, BenniKo and Brathnann like this.
  17. vargata

    vargata

    Joined:
    Nov 26, 2013
    Posts:
    104
    sure it does because I copied your wrong code with the wrong capital letter :D
     
  18. tanmaykulkarni

    tanmaykulkarni

    Joined:
    Nov 5, 2019
    Posts:
    42
    See everyone, I am not trolling or spamming...

    I fixed the problem by just moving my first script (RigidbodyFirstPersonController.cs) to another folder other than Standard Assets. Now I can access my namespace from all scripts. I think Unity compiles Standard Assets folder's scripts in other way as it compiles normal folders..
     
unityunity