1

In the scripts folder, under assets I am trying to define a simple data class:

File: TirggerObject.cs

using UnityEngine;
public class TriggerObject {
    public int TriggerID { get; set;}
    public Vector2 Location { get; set;}
    public float TriggerRadius { get; set;}
}

The class name has a warning associated with it: "Warning: Type should be declared inside namespace 'AssemblyCSharp'"

As far as I can tell the AssembleCSharp is where the class is defined. The only oddity I see is that the solution file tree in the MonoDvelop main UI shows the class file: TriggerObject.cs as being in the assembly "Assembly-CSharp". While the warning message calls it "AssemblyCSharp", without the dash.

I am assuming I can live with this warning since the code seems to run. But, I am struck by how little I find about Unity's assemblies and I can find no reference to this exact warning. I am also not finding any clear explanation of why this is not already in the right assemble or how I can place it in the right assembly.

I hate not understanding warnings.

1 Answers1

1

This is because your file is physically inside the Unity assembly, but that fact is not reflected in your code. I'm afraid this is Unity's "bad automatic practice" when you create your scripts via Unity's Editor. When you create new files via MonoDevelop, the namespace block is automatically generated.

The solution to the problem is to add namespace block to your file:

namespace AssemblyCSharp {

    using UnityEngine;

    public class TriggerObject {
        public int TriggerID { get; set;}
        public Vector2 Location { get; set;}
        public float TriggerRadius { get; set;}
    }
}

You can actually place "using" statement inside or outside the namespace block. Read more about it here.

Please, be aware that adding the namespace section to one file will force you to modify ALL your code files which are dependent (simply due to class visibility).

Ismael
  • 97
  • 1
  • 7