Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Cannot find other classes without assembly definition

Discussion in 'Scripting' started by bawenang, Aug 9, 2023.

  1. bawenang

    bawenang

    Joined:
    Aug 18, 2012
    Posts:
    119
    Hi,

    I've implemented some unit tests in my project because the logic is quite complicated so I need the tests to make sure I get it right. But after I try unit testing, it seems like all the classes in the folder where the logic that is being tested reside (aptly named `/GameLogic/`) can't seem to find other classes outside of their folder. Except if I create another assembly def to the each folder that is being used by the classes in `GameLogic` folder and add them to the asm def references. This eventually made me create asm defs for all folders because I organized the files of my projects into folders based on contexts. And this now has become a tangled web of asm defs.

    So, is this common & expected behaviour? Or is there any way to make this simpler so we don't have to create an asm def for folders that we don't need to test just to add them to the asm defs of the classes that I need to test?

    Thank you.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,941
    Man this is coming up a lot lately.

    Look at the first diagram in the docs about assembly definitions; https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

    The default assembly references all user assemblies, and because we can't have cyclical references, user assemblies can't reference the default assembly.

    If you use assembly definitions, generally all your code needs to be organised in assemblies. This works best when you do it from the get-go because it's very easy to introduce tightly coupled two-way relationships when you aren't enforcing this divide with assembly definitions.

    My advice is to organise by feature or overarching mechanic/system or just general categories. It might be worth making a flowchart to visualise the dependencies, like this one I made when trying to figure out where I had a circular dependency (the error messages aren't very helpful):
    upload_2023-8-9_11-45-41.png
    (Solved it by reversing the relationship between the Camera and Scenes & Levels assemblies).
     
    Bunny83, bawenang and Ryiah like this.
  3. bawenang

    bawenang

    Joined:
    Aug 18, 2012
    Posts:
    119
    So in a way, we need to just do it manually? This is my first time using assembly definitions because the mechanic is pretty complex so I'm not that confident without unit testing. And for unit testing, we need to define the assembly manually and add it into the assembly of the tests, right? I didn't bother to add assembly definitions for my other games because I don't need to test them.
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,941
    I've only really described how assembly definitions work, and this is how they work.

    I've not bothered with Unit tests myself; but I know the tutorial directs the use of assemblies without going into much detail; and it shouldn't need to, there's plenty of documentation on them (that no one seems to read before making these posts).

    A band-aid measure would be to throw all your code into the one assembly and you should be good to go.
     
    Bunny83 likes this.
  5. bawenang

    bawenang

    Joined:
    Aug 18, 2012
    Posts:
    119
    I see. Thanks for the answer.