gameObject.GetComponent param in JS

I made script, that calls a method from other script bound to a gameObject - prefab.

 var sceneManager : GameObject; 
 // ...some logic..
 sceneManager.GetComponent(scriptSceneManager).AddScore();

In Unity script reference there are to types of syntax:

  1. GetComponent(type: Type): Component;

  2. GetComponent(type: string): Component;
    So if I understand correct (possible) the first one gets the type of the component - which is the name of the script?
    And the second one gets the name of the class? But in js scripts there are no classes, right?
    So, when I try to call first type:

    sceneManager.GetComponent(scriptSceneManager).AddScore();
    everything works fine, but when I change “type” to “string:type” (or how it is called):

    sceneManager.GetComponent(“scriptSceneManager”).AddScore();
    I get an error:

‘AddScore’ is not a member of
‘UnityEngine.Component’.
Could anyone help understand this issue?
Thanks.

If you use GetComponent(MyClass), then it gets the MyClass component, typed as MyClass. If you use GetComponent(“MyClass”), the string prevents GetComponent from casting to MyClass, so it just returns Component. This is almost never what you want, plus it’s slower, and has no type safety, so it should be avoided like it was a very dangerous thing that sears your flesh and causes widespread death and destruction. Or, well, just generally avoided anyway.

No, you’re reading those backwards.

  • GetComponent(type: Type): Component

This takes a single argument of type Type and returns the component with that type.

  • GetComponent(type: string): Component

This takes a single argument of type String and returns the component with that name.

Both invocations of the function return a Component, neither returns a type or string.

However, the second invocation doesn’t actually have a Type, so the engine can’t know what to cast the result to, therefore, it simply returns a base Component, whereas the first one does have a Type (since it was passed in), and therefore does an implicit cast on its return value to the Type so passed.

JavaScript… er… UnityScript still does have classes, though it uses a slightly different form of inheritance. All Scripts inherit from Components, which is why they can be added to GameObjects.