Undefined Error After Assigning Parameter in Editor

Hello.

I am having an issue with a parameter returning as “undefined”. (I am new to Typescript, so this might just be an easy oversight on my part, but I have searched and cannot find any solution.)

I have two classes: Random_Tilt & Title_Controller

Random_Tilt has the following code:

@property(Title_Controller)
titleController: Title_Controller;

(It also automatically imported the Title_Controller class already - See Below)
image

In the editor, I then dragged the node that has the class Title_Controller attached to it (It’s called “Game Controller”). (See Below)
image

I then try to “use” the parameter and get an “undefined” error.

For example, the following code would return an error:

console.log(this.titleController.node.name);

Does anyone have any ideas on what I might be overlooking?

I went ahead and tried to bypass the issue by changing:

@property(Title_Controller)
titleController: Title_Controller;

over to:

@property(Node)
titleControllerNode: Node;

titleController: Title_Controller;

and then call:

    onLoad() {
        this.titleController = this.titleControllerNode.getComponent(Title_Controller);
    }

However, I still get the same undefined error. Now I’m really confused! >.<
The node (Game Controller) definitely HAS the Title_Controller class attached to it. I’m probably missing something obvious. :man_shrugging:

There won’t be any issues in my test, please check my project.

59551.zip (565.1 KB)

Your example helped me a lot! I created some new scripts like yours and everything worked fine, so I went back to see just why my original wasn’t working. It turns out that I was incorrectly calling a function from an onComplete property of a tween. This was my original code:

        let tweenPos = tween(this.node)
            .to(this.speed, { position: this.originalPosition }, { onComplete: this.ReportFinished, easing: "quartOut" })

When written this way, it would trigger the ReportFinished() method, but the method would not have a reference to any of my variables, so everything would be “undefined”. I remembered that you helped me on a previous post and changed it to the following:

        let tweenPos = tween(this.node)
            .to(this.speed, { position: this.originalPosition }, { onComplete: this.ReportFinished.bind(this), easing: "quartOut" })

I didn’t realize that I still needed the “.bind(this)” portion when there was no variables, but now I understand! So, thank you again! :slight_smile:

1 Like