GetComponent using a string return NULL but using a type return something ?

Hi,

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.

Thanks!

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 )). :slight_smile:

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.

How else would you expect it to know which type you mean? If you have different types with the same name?

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.

1 Like

If you have an asset with a java script file, you can get the component from a C# script with:

Component _component= gameObject.GetComponent ("Interruptor") as Component;
MonoBehaviour interruptor = _component as MonoBehaviour;

I could be wrong, but I think this is the only way to do this in this situation

True. That probably explain why it returned NULL then. It won’t know in which namespace to pick the right one.

Like i said i am not using it. Its an asset that was using it because it use reflection to work.

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.

1 Like