I have a component on a gameObject called Interruptor. Its a script.
If i use GetComponent(“Interruptor”) it return null but if i do GetComponent it return the Interruptor type.
Is there a reason GetComponent won’t return it ? It need to be called this way… since its not my code its in an asset which cause problem because of this. I tried the same thing within the Interruptor script and that do the same thing so the asset are not in cause.
I don’t quite understand, why do you want to use a string?
Anyways, the only two ways to use GetComponent (atleast that I know of) is calling GetComponent< ObjectName >() or GetComponent( typeof( ObjectName )).
Well if it was me i would use a type instead but the problem is that its an Asset i use that use GetComponent with a string and causing this problem. I somewhat figured out why i have this problem but its hard to fully replicate it. It seem to happen if 2 types have the same name but in the case its happening these 2 types was in different namespace so its hard to believe its happening. If they were on the same namespace i would understand but its not the case. I tried a standalone project where one type was called Interceptor in Asset namespace and the other in Asset.Scripts namespace and the bug was happening. GetComponent with a string could NOT find any component.
However, if i include the correct namespace in the string it get found without problem.
The string based function is pretty unreliable anyway, I’d suggest not using it at all. You have found a situation where its type conversion fails.
That said I’d encourage you not to name classes the same in different namespaces. Namespaces are designed to prevent accidental name collision. Its not a good idea to use them to separate deliberate name collisions.
So the string method just searches the assembly for a class whose name matches the string. When the offending code passes in a name that multiple classes have, how is it supposed to know which to return? This is why when you include the full namespace in the string it matches directly and correctly to the unique class.
Why can’t you just fix the offending code in the asset? Fix it, then tell the asset author how you fixed it, and that they should change their method of doing it to avoid this problem in the future.