Search Unity

Can't properly reference public classes from the Assembly-CSharp into Assembly-CSharp-Editor

Discussion in 'Scripting' started by Fkcn, May 6, 2020.

  1. Fkcn

    Fkcn

    Joined:
    Apr 30, 2020
    Posts:
    2
    This issue has been bugging me for days and lots of hours lost. Already checked the known issues on the release notes.

    Might as well post it here to ask for help, so that you guys can tell me, if I missed something.

    As the title says, I can't properly reference classes from the Assembly-CSharp into Assembly-CSharp-Editor.
    Upon checking, the project Assembly-CSharp is a dependency of the Assembly-CSharp-Editor. With that said, I expect the public classes from the Assembly-CSharp would be accessible as intended.

    Listing 1
    Code (CSharp):
    1. // Declared in Assembly-CSharp
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace Tool {
    7.  
    8.     public class CubicBezierTool : MonoBehaviour {
    9.     }
    10. }
    11.  
    Listing 2
    Code (CSharp):
    1. // Declared in Assembly-CSharp-Editor
    2. using UnityEditor;
    3. using UnityEngine;
    4. using Tool; // Namespace doesn't get recognize
    5.  
    6. [CustomEditor(typeof(CubicBezierTool))] // Absolutely, this wouldn't be also recognized
    7. public class BezierHandle : Editor {
    8.  
    9.     private void OnSceneGUI() {
    10.  
    11.         CubicBezierTool tool = (CubicBezierTool)this.target;
    12.  
    13.         Handles.Label(tool.transform.position, "S***");
    14.     }
    15. }
    16.  
    As seen in the snippet posted, you guys can assume the project would be built smoothly, but it wasn't; because on the listing #2 line 4, the compiler doesn't recognize immediately the namespace Tool and also the class CubicBezierTool at line 6

    If someone can help me, that would be really great.

    BTW I'm using Unity 2019.3.12f1 and Visual Studio 2019 Community Edition
     
  2. n_i_e

    n_i_e

    Joined:
    Mar 20, 2013
    Posts:
    3
    I think it's possible we are having similar issues, and I was literally about to post something on these forums for the first time in years because I have been tearing my hair out over this.
    My issue is that, like you, my editor scripts do not recognize non-editor script namespaces in Visual Studio. Despite this, my code does run. (Does yours?)

    Since my code does run I figured this was a Visual Studio specific issue where for whatever reason it simply is not recognizing namespaces. To test this I tried opening up this code in VSCode, and as I suspected VSCode correctly recognizes these namespaces as you would expect them to.

    As you've said,
    (One thing I've noticed is that for me it's backwards. It says Assembly-CSharp-Editor Depends on: Assembly-CSharp. Maybe that's your issue unless you misspoke.)

    Obviously this has something to do with the "Assembly-CSharp-Editor" simply being unable to see stuff that's in "Assembly-CSharp". So I guess the question is how to force Visual Studio to see it.

    Looking online I have found one thing which seems relevant to this case:
    https://answers.unity.com/questions/1119176/why-cant-some-of-my-scripts-see-other-scripts-or-n.html

    The interesting thing about this post to me is the response here:

    While I'm not sure if that is exactly the same case, there may be something worth considering.

    Also I have tried reinstalling Visual Studio and Unity multiple times, as well as starting a new Unity project from scratch -- all to no avail.

    I am using:
    Unity 2019.3.12f1
    Microsoft Visual Studio Community 2019 Version 16.5.4


    Edit:
    I may have found a solution.

    In your assets folder create an "Assembly Definition File"
    In your editor folder create an "Assembly Reference File"
    Click the assembly reference file and assign your Assembly Definition.

    This fixed it for me as it basically joined the two assemblies together in Visual Studio.

    HOWEVER... I don't understand why none of the Editor Scripting tutorials I've seen have said anything about this, which makes me wonder if this is even what you're supposed to do. Is this something that is supposed to work by default? Like I said before, my code still runs, it just behaves weird in Visual Studio.

    Some Unity docs I found about Assembly References & Definitions:
    https://docs.unity3d.com/2018.3/Documentation/Manual/ScriptCompilationAssemblyDefinitionFiles.html
    https://docs.unity3d.com/Manual/ScriptCompileOrderFolders.html
     
    Last edited: May 7, 2020
    TakuanDaikon and Fkcn like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    One easy way around this is to move your the editor script(s) that need access to your main game out of the Editor folder and then guard the entire class file contents with #if UNITY_EDITOR so the build still builds. It's ugly but it works.
     
  4. Fkcn

    Fkcn

    Joined:
    Apr 30, 2020
    Posts:
    2
    Yes, my code doesn't run because the compiler doesn't recognize the classes declared on the Assembly-CSharp.

    (One thing I've noticed is that for me it's backwards. It says Assembly-CSharp-Editor Depends on: Assembly-CSharp. Maybe that's your issue unless you misspoke.)

    Yes, I really find it weird the Assembly-CSharp is a dependency of Assembly-CSharp-Editor, but can't see any of the publicly declared classes in the Assembly-CSharp. Is it should be the other way around? The ACE is a dependency of the AC?

    Well, I've tried your suggestion by using the Assembly Reference and Definitions; and it is working now. I just need to review what the heck it is so that I understand it well.

    Thanks!
     
  5. snowfy5

    snowfy5

    Joined:
    Jun 17, 2019
    Posts:
    4
    Hey, I'm having the same issue with "Assembly-CSharp-Editor Depends on: Assembly-CSharp", but no luck referencing Assembly-CSharp scripts from my Editor folder. It's giving me an error in both VS and the Unity Editor.

    In the manual, it states that Assembly-CSharp-Editor gets compiled last. It also states that "Anything that is compiled in the current phase or an earlier phase is fully available."

    Did you happen to figure out why it is working like this?
     
  6. lcheers

    lcheers

    Joined:
    Jun 6, 2015
    Posts:
    2
    This just happened to me; it seems to be a bug caused by Unity not updating something properly.
    Following the suggestion above, I added an Assembly Definition file - which fixed the problem but broke my builds by combining the editor scripts into my main assembly. Fortunately, after I deleted the Assembly Definition file again, Unity fixed itself and now everything is back to normal.
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,006
    In my 10 years of using Unity (probably around 20 to 30 different versions) I never had any issues like that, ever. I'm always wondering how you guys manage to get such behaviours. I've seen people switching too fast between VS and Unity in quick succession while simultaneously doing 10 other things. Yes, this can cause all sorts of issues. When you switch to Unity, let it compile the scripts. Even though I personally never had and issues with anti virus software, over the years there has been quite a few people reporting problems. Depending how the AV does the live monitoring it could cause issues.
     
  8. daochihao98

    daochihao98

    Joined:
    Apr 16, 2020
    Posts:
    5
  9. unity_l0KDDLBYa8hWhA

    unity_l0KDDLBYa8hWhA

    Joined:
    Feb 24, 2020
    Posts:
    8
    Wow thank you for your helpful comment everybody certainly asked for <3
     
  10. unity_l0KDDLBYa8hWhA

    unity_l0KDDLBYa8hWhA

    Joined:
    Feb 24, 2020
    Posts:
    8
    VS was not able to reference one of my scripts as well. What solved the problem for me was moving it to another folder once, letting everything reload, and then moving it back again.