[SOLVED] Cannot read property 'enabled' of null

My botAI script in my Feline Fighters game all of a sudden decided to not work any more and give me an error, cannot read property ‘enabled’ of null on line 29. Can someone look at it and see if you can figure it out… This worked perfectly fine last night.

Here is the link to my game

You build up a list of targets like this:

        this.targets = [];
        this.players = app.root.findByName("Players").getChildren();

So this.targets is an array of pc.Entity objects.

You then do:

this.target = this.targets[Math.floor((Math.random()*this.targets.length))];

So this.target is a pc.Entity.

The line where the exception is thrown is:

        if(app.root.findByName(this.target).enabled === false){

So you’re passing a pc.Entity to the findByName function. If you check out the docs, you’ll see the function takes a string (entity name), not a pc.Entity.

Thanks Will, it works now!

1 Like