[SOLVED] Space Pirate Game: Cannot read properties of null (reading 'resource')

Here is the link to my editor:

Getting an error when launching and also my enemy craft disappears when launched.
I am a teacher and we are following a curriculum for PlayCanvas, so these errors are brand new to me and a lot of students are getting the same, Trying to educate myself on how to fix or what to look for when this happens.
Thank you!

Hi @Ashley_Solano! Are you sure there is an asset with the name ‘EnemyCraft’ in your project?

I just renamed the EnemyShip to EnemyCraft and am getting the same error.
When I click the error, it is taking me to this line.
var instance = templateAsset.resource.instantiate();

As far I can see the asset has the name “EnemyShip” while you use “EnemyCraft” in your script.

gotcha, okay changed script to EnemyShip.
Okay it is working! My Enemy Ship is not randomly spawning however.
Thank you for the help!

1 Like

Do you know what is missing for my EnemyShip to Spawn at random?

One of the problems is that you use a variable before you define it.


On line 31 randomspawn is undefined.

1 Like

What would I need to do to define it? Sorry I am completely lost. :upside_down_face:
I have this in the lesson but not sure if that means in the editor or in the script. Thanks for the help

You define randomspawn on line 33 but you already use it on line 31, so that’s in the wrong order.

TY I moved the variable up and now I am getting cannot read undefined ‘x’

Probably because you didn’t define position. What do you think position is in this case?

The position of the enemy craft on the scene, right?
Sorry, I am working with this curriculum and some of it is pre done and it doesn’t say that I needed to add anything else and it should work, but I keep getting these errors so trying to see what I am missing.

Is the enemy craft in the scene the instance or the entity with the script?

To get for example the x position of the entity with the script, you can do like below.


To define position in your case, you can do like below.

var position = this.entity.getPosition();

Okay, that was already in there. I just had things in the wrong order. Thank you for your time, I really appreciate it!

1 Like