Babylon Unity Toolkit - accessing other typeScript files within a Scene


I am trying to access other typeScript files within my scene, so that I can trigger certain elements.
I’ve tried to access via: import { Example } from ‘exampleClass’;
unfortunately I get this error:
/Assets/Scripts/exampleClass.ts’ is not a module.

Is there maybe a different way of accessing other typeScript files within my scene?

Maybe @MackeyK24 can help here again :slight_smile:

Yup @MackeyK24 is the unity toolkit guru :slight_smile:

First off, the toolkit use a tsconfig.json that gets created on first build. So all your script component are aware of each other and you dont need to do imports in your script component.

To get reference to script components attached to entities at runtime you would use either the script component shortcut function to get the component. So from a BABYLON.ScriptComponent:

const animator:BABYLON.AnimationState = this.getComponent("BABYLON.AnimationState");

would get a reference to the Unity Style Animator component that is also on the same game object your script is on. Just like how unity does GetComponent.

A more general purpose function you could always use to get a reference for any script component attached to any game object that you could call from anywhere in your project (not just the entity your script is attached to):

const animator:BABYLON.AnimationState = BABYLON.SceneManager.FindScriptComponent(transformNode, "BABYLON.AnimationState");

You can then make function calls to the Unity Style Animator component to update an animation parameter, example:

animator.setFloat("Speed", this.speed);
animator.setBool("Grounded", this.grounded);

You might use to cause the animation states to transition to the proper animation state based on transitions and conditions setup in the Unity Animation controller.

Example script component:

module PROJECT {
    * Babylon Script Component
    * @class MyTest
    export class MyTest extends BABYLON.ScriptComponent {

        protected awake(): void {
            /* Init component function */
            const animator:BABYLON.AnimationState = this.getComponent("BABYLON.AnimationState");
            if (animator != null) {
                animator.setFloat("Speed", 0);          // Note: Set initial speed parameter to zero
                animator.setBool("Grounded", true);     // Note: Set initial grounded state to true

        protected start(): void {
            /* Start render loop function */

        protected ready(): void {
            /* Execute when ready function */

        protected update(): void {
            /* Update render loop function */

        protected late(): void {
            /* Late update render loop function */

        protected after(): void {
            /* After update render loop function */

        protected fixed(): void {
            /* Fixed update physics step function */

        protected destroy(): void {
            /* Destroy component function */

You can also safely removed any unused life-cycle functions. The template is to show you all the functions of the script components life cycle you can use. Like Unity’s Monobehavior class

Hope that helps :slight_smile: