Search Issue Tracker
Won't Fix
Votes
1
Found in
2017.2.0a4
2018.1.0f2
Issue ID
1033982
Regression
Yes
Importing a class with the same name as a built-in Unity component gets incorrect import warning
To reproduce:
1. Download attached project "New Unity Project (9).zip" and open in Unity
2. Reimport "Grid.cs"
3. Observe that you will get Warning in Console:
"Script 'Grid' has the same name as built-in Unity component
AddComponent and GetComponent will not work with this script"
Notes:
- This issue appears both on Windows and OSX
- A grid is a System.object and not a Component so AddComponent/GetComponent isn't applicable towards this class anyway
Reproduced on Unity 2017.2.0a4, 2017.2.3f1, 2017.3.2f1, 2017.4.2f2, 2018.1.0a1, 2018.1.0f2, 2018.2.0b3 and 2018.3.0a1
Not Reproduced on Unity 2017.1.3p4 and 2017.2.0a3
Regression on Unity 2017.2.0a4
-
Hurri04
Mar 26, 2021 11:46
Relevant thread:
https://forum.unity.com/threads/script-halo-has-the-same-name-as-built-in-unity-component.629008/#post-5027504The overloads of the AddComponent and GetComponent methods which take in a string parameter with the Type name have already been deprecated and in fact calls to them automamatically get replaced in the source code (see my comments in the submitted example project, Case 1324045).
Further the Generic Constraint "where T : Component" has already been added to the AddComponent<T> method.
This means that the 2 remaining steps to fix this issue are:
1. Add the Generic Constraint "where T : Component" to remaining methods GetComponent<T>, GetComponent(s)InChildren<T>, GetComponent(s)InParent<T>, etc. This will prevent being able to even attempt to call them with a custom class as Generic Parameter (e.g. "GetComponent<Grid>();") if this custom class derives from System.Object, as this would then throw a proper Compiler Error instead.
2. At the place where the Warning is thrown add a simple check for "typeof(T).NameSpace" and only proceed to throw the Warning if there is no namespace. To make the Warning message clearer, change it to state that in this case there is ambiguity with the built-in Unity component and that the user should fix this by adding a custom namespace.
And that should be enough to fix this issue.
-
oobartez
Feb 03, 2019 20:48
This is a serious and ridiculous bug. How come you cannot use a name such as "Terrain" when developing a video game.
C# already has a feature to support this scenario, it's called namespaces. In case Unity's developers are not familiar with, let me refer them to the documentation:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/namespaces/
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Missing Render Feature "Full Screen Pass Render Feature" in any “Universal Renderer Data” asset when upgrading from 2022.3
- Inconsistent ParticleSystemVertexStream.PercentageAlongTrail data range in Trail Texture Modes except "Stretch"
- The Graph Debug Window can be right clicked through and the Node Workspace is manipulated instead
- [Linux] Top left corner of the screen is unresponsive when the Editor recompiles
- [Android] [Vulkan] Cubes stuck on the first few frames of rotation and application flickering when an Overlay Camera is added to the Camera Stack with MSAA enabled
Resolution Note:
This particular case has been investigated thoroughly and we have decided, in the interests of protecting the stability and features of Unity for users that rely upon the affected versions, to not address this issue.