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

Question Script '<some name>' has the same name as built-in Unity component.

Discussion in 'Editor & General Support' started by AngryProgrammer, May 22, 2021.

  1. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    The powerful oracle of Unity! I need your help...
    1. Can I somehow get rid of the warning: Script '<some name>' has the same name as built-in Unity component? Will wrapping a class into a namespace protect code from unexpected behavior from Unity?
    2. I can prefix the name. Unfortunately, something like this looks ugly in the inspector. Is there any attribute similar to [InspectorName ("name")] that can give me alternative name to display for class?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,903
    You really should always use namespace and clear your console. This warning is there to inform you to look out for GetComponent calls. Because it will be confusing for you when you will find what components exactly once you put your code in namespace(s).

    It's a bad idea to name your own components the same as Unity calls theirs. Choose the prefix or name them something else.

    No. You can buy Odin Inspector and hide the MonoScript header if you want. Or you can write your own inspector.
     
    AngryProgrammer likes this.
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,114
    Yeah it looks like there unfortunately is no way around this; you just have to rename the class or ignore the warning. Even using a unique namespace won't make the warning go away.

    The AddComponentMenu attribute can be used to customize what is shown in the add component menu, but I don't think there's any attribute to easily change the label shown in the inspector unfortunately. You might be able to pull this off by creating a custom editor for the class however.
     
    AngryProgrammer likes this.
  4. dan-oak

    dan-oak

    Joined:
    Jul 14, 2020
    Posts:
    9
    It doesn't even need to be a mono behaviour. You can have ` internal class Terrain {} ` and that's it, Unity will complain:

    Script 'Terrain' has the same name as built-in Unity component.
    AddComponent and GetComponent will not work with this script.

    I do not care about AddComponent and GetComponent in this context. It's not even a public class. Can Unity leave me alone with my code please?

    Warnings are meant to warn about something potentially erroneous. Not to recite a random line from the documentation.

    I may sound harsh, but it's important to us. We keep our codebase as clean as possible, and there is no error or warning ignored to keep the code in it's best quality. With this single warning wen need to check the console every single time to confirm that there is no other warning _except_ this one.

    Unity, please let us have an option to silence this warning. For example Odin plugins have a settings menu where such warnings are silenced once developers learned about them.
     
    Last edited: Jan 30, 2022
    muzappar2 likes this.
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,114
    If I remember correctly, you can get rid of the warning with plain old class objects by just changing the name of the file to something else - the actual class name can still be the same one that an internal Unity class has.

    As for components I've discovered since my last comment that the AddComponentMenu attribute actually will nowadays also change the name that is shown in the Inspector. So I think you could nowadays have a class like
    LightComponent : MonoBehaviour
    and use
    [AddComponentMenu("My Components/Light")]
    and it would just appear as "Light" in the Inspector and there shouldn't be any warnings about it conflicting with the internal Light class.
     
  6. TenaciousDan

    TenaciousDan

    Joined:
    Jul 11, 2019
    Posts:
    22
    SisusCo's answer is probably the best workaround I've found.
    It is however, unfortunate, that this warning exists. I noticed that it only appears when refactoring the file containing the class with the similar name.
    This warning is only there to support GetComponent(string) and AddComponent(string) and warn the user against potential problems.
    It would be nice if:
    1. There was a way to suppress the warning via code perhaps by adding a special attribute (ex: [UnitySuppressWarning(WarningID)]) before the class declaration.
    AND/OR
    2. The warning is only logged when a use of GetComponent(string) or AddComponent(string) is detected (if possible).

    In my opinion, warnings should have an id associated with them and a lookup table can be kept so that the user can silence/suppress these types of warnings via attribute.
     
  7. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    It's been a long time since the problem mentioned above. I'm currently working on version 2021.3 (which wasn't LTS at the time), no more naming errors, no conflicts, no need to create weird prefixes.

    The problem has become a thing of the past.
     
  8. TenaciousDan

    TenaciousDan

    Joined:
    Jul 11, 2019
    Posts:
    22
    I am still getting the warning in 2021.3.14f1 .
    The warning only shows up when I make a change to the file containing the class with the similar name. If I don't modify that file then I do not see that warning. But I still think I should have the option to silence it and any other future warning that may come up that I wish to silence.
     
  9. ClockStone_Helmut

    ClockStone_Helmut

    Joined:
    Feb 23, 2015
    Posts:
    12
    Still there in 2021.3.18f1. This is really annoying for normal classes that are inside a custom namespace.