Uncaught TypeError: Cannot read property 'root' of undefined

Hi. I am trying a 2D plane disappear after 1 second. However, when I try to do this, I get this error:
Uncaught TypeError: Cannot read property 'root' of undefined.

Here is my code:

var app = this.app;

       var screen4 = app.root.findByName('Frame').findByName('Screen4');
       screen4.enabled = true;
        setTimeout(function () {
            screen4.enabled = false;
        }, 1);

Any help? :slight_smile:

this.app to access the application instance is available in pc.script functions. It looks like you are trying to access it in the global where this.app is undefined.

Given the topics you have opened in recent days, I really recommend going through the Keepy Uppy tutorial series that goes through using PlayCanvas https://developer.playcanvas.com/en/tutorials/keepyup-part-one/

It will help in giving more concrete examples of using the PlayCanvas API.

Hey! Thanks for the response, I will make sure to go through the the tutorial series.
But how would I be able to fix the error? :slight_smile:

The issue is less about the code and the syntax and more on the understanding of context, scope and where you have access to certain objects. this.app is available within the scope of a pc.script function and Iโ€™m guess you are trying to use it in global scope where it would be undefined.

How much do you know about local, function and global scope along with what the this keyword means?

Not very much. I think in global means that it is outside of a scope and that you can also use it inside a scope, but thatโ€™s as much as I know :confused:

It is worth reading up on, especially about the this keyword as it helps to understand how the PlayCanvas classes and objects work together.