# \[Unity-\>BJS\] How get a reference to the .ts script sitting on another game object

**URL:** <https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666>\
**Category:** Questions\
**Created:** [July 20, 2020, 11:52am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666 "2020-07-20T11:52:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 20, 2020, 11:52am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/1 "2020-07-20T11:52:48Z")

</div>

Hi there,

How can I refer to B.ts from A.ts, if A and B are on different Unity gameObjects?

---

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 20, 2020, 1:58pm UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/2 "2020-07-20T13:58:08Z")

</div>

@MackeyK24 I am pinging you - this is somewhat basic I could not find info/playground on this anywhere. Thank you in advance!

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [July 21, 2020, 1:54am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/3 "2020-07-21T01:54:30Z")

</div>

GetComponent function call

From a Script Component

```auto
const myscript:PROJECT.MyScriptClass = this.getComponent("PROJECT.MyScriptClass");

```

---

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 21, 2020, 7:41am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/4 "2020-07-21T07:41:22Z")

</div>

> [@MackeyK24](#):
>
> `const myscript:PROJECT.MyScriptClass = this.getComponent("PROJECT.MyScriptClass");`

Thank you very much for the answer and great tool.

---

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 21, 2020, 8:21am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/5 "2020-07-21T08:21:16Z")

</div>

@MackeyK24 Little update here: it works if both scripts are on the same game object. If they are on different game objects, the project compiles, but I get this error at runtime:

_Uncaught TypeError: myscript is null_

I put the code you wrote in the start() void.

Maybe this has something to do with script compilation order? I have tried moving all Editor Script Components to the folder “Editor” (Unity practice), but this breaks the project.

---

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 21, 2020, 9:08am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/6 "2020-07-21T09:08:56Z")

</div>

And if I try to put that code in the constructor or ready() void, I get this:  
_[11:08:16]: Unable to load from scenes/Main.babylon: Error in onSuccess callback_

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [July 21, 2020, 10:19am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/7 "2020-07-21T10:19:53Z")

</div>

If on a different script use the this.manager.findSceneComponent

---

<div class="post-metadata">

**Author:** ![enquel](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@enquel](https://forum.babylonjs.com/u/enquel)\
**Post date:** [July 21, 2020, 11:10am UTC](https://forum.babylonjs.com/t/unity-bjs-how-get-a-reference-to-the-ts-script-sitting-on-another-game-object/12666/9 "2020-07-21T11:10:05Z")

</div>

Thank you, works like a charm!
