How do you get a script component on a gameobject?

I’m trying to get a script component from another script. Both scripts are attached to the same gameobject.

I tried:

GetComponent<TheScriptIWantToGet>();

But I’m getting…

Severity    Code    Description    Project    File    Line    Suppression State
Error    CS0246    The type or namespace name 'TheScriptIWantToGet' could not be found (are you missing a using directive or an assembly reference?)

How do you do this?

You’re doing it correctly. Some would write gameObject.getComponent to make it more obvious it’s a script on you, but that’s not needed. There’s likely a problem with your script name. Misspelled, or it’s inside another namespace, or your code editor is hooked up wrong (Unity itself will be fine, and it will run, but intellisense falsely claims it’s not there).

As a test, you might try to find your own script. If the computer can’t find that name, something is very wrong.

1 Like

Ah, this is the problem…

1 Like