Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug RequireComponent and Namespaces: Bug or Expected Behavior?

Discussion in 'Scripting' started by ShortFox, Jun 2, 2021.

  1. ShortFox

    ShortFox

    Joined:
    Jan 10, 2014
    Posts:
    1
    I am working on a short project where I defined a class named "Agent". So far, I have not been encapsulating my project in a namespace. In the future, I will incorporate Unity's ML-Agents into the project. Their Unity.MLAgents namespace also contains a class called Agent with the attribute [RequireComponent(typeof(BehaviorParameters))].

    I imported MLAgents but have not touched it. However, when I run my Unity scene, I see that "BehaviorParameters" becomes added to my gameobjects with my implementation of the Agent class. I tested if this repeats when I wrap my Agent class in a namespace, and the same behavior does not occur (i.e., BehaviorParameters is not added).

    It appears that RequireComponent acts upon instances of a class if it is wrapped in the same namespace or if there is no namespace wrapper. To me this appears to be a bug with how RequireComponent should operate. I would expect any relevant component would be added to objects with that specific namespace's implementation of the class.

    On my end, I see the possible solutions to include: Change my class name "Agent" to something else to avoid conflict, or to wrap my code in a namespace (which I know is probably best practice).

    I am relatively new to namespaces so I'd be interested to know if this is expected behavior - otherwise my intuition is that this is a bug to be corrected. Thoughts?
     
  2. Emery-Monzerol

    Emery-Monzerol

    Joined:
    Nov 1, 2012
    Posts:
    20
    Hey, my team got the same issue, but with a script of our own called FirstPersonController, "conflicting" with the Standard Assets' FirstPersonController. Unity's FirstPersonController has
    [RequireComponent(typeof(CharacterController))]
    , and ours doesn't. We haven't had issues for a very long time, but recently we noticed that our prefab which has our own FirstPersonController component automatically added a CharacterController component to itself, and kept re-adding it if we removed it manually.

    We're fairly confident that this is a Unity bug. We decided to just rename our own class to something else in order to avoid the issue. While it is good practice to have your own namespaces, I don't think this is expected behaviour at all, since both classes are already not in the same namespace, which means there shouldn't be any conflict between the two.
     
  3. pgrenon

    pgrenon

    Joined:
    Oct 26, 2016
    Posts:
    3
    Yes, I have a clean repro:
    1. Create a MonoBehaviour script called "Test", put it in a namespace and add [RequireComponent(typeof(BoxCollider))] to it.
    2. In a different folder, create a MonoBehaviour script called "Test", leave it as it is created.
    3. Open a prefab in the prefab stage, add the un-namespaced version of the "Test" script to it and save the prefab.
    Result: You get a warning that says that a BoxCollider was added to the prefab. A BoxCollider is not visible on the prefab stage, but if you check the asset (or reopen the prefab in the prefab stage), and while your "Test" component still points to the un-namespaced version of the "Test" script, a BoxCollider was added.

    EDIT: Tested in Unity 2020.3.11f1
     
    ShortFox and Emery-Monzerol like this.