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.

Hey man, did you solve the problem? I got the same error.

Can you post your code and/or your project?

I don’t have acces to my laptop right now, but it is basically the same problem that @_Lio3LivioN encountered.

This error can come up from many different ways.

From a guess, you are either using the this.app in a callback function and therefore the this object is no longer referencing the script instance.

Or app.root is being used with app being defined. In which case, using this.app.root might fix it.

Again, we can’t provide a proper answer with seeing your code.

Oh I am very dumb.
I tried using findByName without putting it in script.prototype.initialize(). I am getting started with playcanvas, so I’m learning.
Anyway thanks for the help, this community is very helpful

1 Like