Enabled and disabled problem

    if(this.count==2){
        this.app.root.findByName('fin2').enabled=true;
        this.app.root.findByName('fin4').enabled=false;
        this.app.root.findByName('fin8').enabled=false;
        this.app.root.findByName('fin16').enabled=false;
        this.app.root.findByName('fin32').enabled=false;
        this.app.root.findByName('fin64').enabled=false;
        this.app.root.findByName('fin128').enabled=false;
        this.app.root.findByName('fin256').enabled=false;
        this.app.root.findByName('fin512').enabled=false;
        this.app.root.findByName('fin1024').enabled=false;
        this.app.root.findByName('fin2048').enabled=false;
    }

I set up a very simple logic but I keep getting the same error

[Controller.js?id=57199393&branchId=6a3319de-9f47-48f1-8882-5e31e6af8088:277]: Uncaught TypeError: Cannot set properties of null (setting ‘enabled’)

That usually means the findByName() method didn’t manage to find an entity and returned null.

Check your names and if that entity exists when the method runs.

2 Likes

So how can I do

Hi @Ozden_Delibas! Check in your project if the entity that the script needs to find, exist and has the exact same name.

The name or something is correct but I don’t understand why she’s doing that

Can you share a link of your project please? Then I can take a look.

https://playcanvas.com/editor/scene/1260551

I see the entities are indeed in your scene, but I think the problem here is the order of your script. It looks like you are changing the scene right before you execute these error lines. Try to change the order of your script to see if it solves the problem.

oldHierarchy.destroy();
this.loadScene(this.sceneId,function(){});

It’s the first time I’ve seen a code sequence matter. But still thank you.

I see the error still exist on another point in your game. I would add an extra statement to prevent the code is running when there is no hierarchy. If that doesn’t solve the problem, I don’t know what could be the reason for the error.

if (this.app.root.findByName("Root")) {
    if(this.count==2){
        this.app.root.findByName('fin2').enabled=true;
        this.app.root.findByName('fin4').enabled=false;
        this.app.root.findByName('fin8').enabled=false;
        this.app.root.findByName('fin16').enabled=false;
        this.app.root.findByName('fin32').enabled=false;
        this.app.root.findByName('fin64').enabled=false;
        this.app.root.findByName('fin128').enabled=false;
        this.app.root.findByName('fin256').enabled=false;
        this.app.root.findByName('fin512').enabled=false;
        this.app.root.findByName('fin1024').enabled=false;
        this.app.root.findByName('fin2048').enabled=false;
    }
}