Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

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

Discussion in 'Scripting' started by NoradZero, Nov 8, 2015.

  1. NoradZero

    NoradZero

    Joined:
    Apr 24, 2014
    Posts:
    95
    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<Interruptor> 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!
     
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    203
    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 )). :)
     
  3. NoradZero

    NoradZero

    Joined:
    Apr 24, 2014
    Posts:
    95
    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.
     
  4. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    How else would you expect it to know which type you mean? If you have different types with the same name?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,734
    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.
     
    Zenix likes this.
  6. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,698
    If you have an asset with a java script file, you can get the component from a C# script with:

    Code (csharp):
    1.  
    2. Component _component= gameObject.GetComponent ("Interruptor") as Component;
    3. MonoBehaviour interruptor = _component as MonoBehaviour;
    4.  
    5.  
    I could be wrong, but I think this is the only way to do this in this situation
     
  7. NoradZero

    NoradZero

    Joined:
    Apr 24, 2014
    Posts:
    95
    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.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    7,352
    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.
     
    Kiwasi likes this.
unityunity