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. Dismiss Notice

New assembly generates error: The type or namespace name could not be found.

Discussion in 'General Discussion' started by Royy212, Aug 7, 2023.

  1. Royy212

    Royy212

    Joined:
    Aug 1, 2016
    Posts:
    33
    I've only defined one assembly, I've put this assembly and one script (that generates the errors) in a sub folder, together.

    This is the only assembly in my whole project that is not predefined.
    But currently I'm getting 4 errors for scripts that could not be found, these scripts are scripts that I've made myself, the scripts are missing for the script that is trying to use them.

    All Unity defined classes are perfectly accessible for the script that is generating the errors, these classes do not generate an error: Color, Random, WaitForSecondsRealtime etc.
    But the classes I've made and I'm trying to use in the script, do generate an error.

    I've tried to search for an assembly to add to the Assembly Definition References, but there are none I should need to add AFAIK since all my scripts (but one) are not in any self-created assembly, they are all sitting in the Assembly-CSharp.dll which I can't add to the Assembly Definition References.

    I also tried Reimport All without luck.

    EDIT: Creating a new assembly for the scripts that are not found and then adding that newly created assembly as a reference to my first created assembly will fix the errors. (The classes are now found.)

    But this means, I've to put all my scripts that are referenced by scripts sitting in an assembly in their own assemblies. This sounds weird to me, is this normal or a bug?
     

    Attached Files:

    Last edited: Aug 7, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    This is normal.

    If you have two scripts A and B and B references A (eg it has a field "public A a;") and you place B in an assembly definition, you need to tell that assembly definition where it can find A. But since A is not in an assembly definition but the global assembly, it cannot be referenced by the assembly definition.

    The auto referenced checkbox works the other way round: if A references B then that would work because the dependent assembly definition B is auto-referenced by the global assembly that A resides in.

    The major point of assembly definitions is to divide your code into a dependency hierarchy while making it impossible to create circular dependencies between assemblies, which is the bane of all software architectures. You can still have circular dependencies within an assembly but at least that's "contained" and thus easier to fix.
     
    Royy212 and Ryiah like this.
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Completely normal.

    You can see how this works in the docs: https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html

    The first diagram shows that the default Assembly-CSharp references any user assemblies. Thus user assemblies can't 'look backwards' at the default assembly.

    Generally, if you're using assembly definitions, all your code needs to be organised with assemblies.
     
    Royy212 likes this.
  4. Royy212

    Royy212

    Joined:
    Aug 1, 2016
    Posts:
    33
    This is what I needed, thank you both!
    I was under the assumption that scripts not placed in an assembly were referenceable through an assembly I created myself and by checking the auto referenced box everything would 'fix' itself. Now I know better.