[SOLVED] Undefined when finding an entity within an instantiated template

Hi all. I have a template which is instantiated at runtime. Once its instantiated I try to find one of the entities contained within it that is called ‘Acc_Pos’ and also has a tag called ‘acc_position’

However if i do either of the following I just get undefined:

this.acc_position=this.app.root.findByTag("acc_position");
or
this.acc_position=this.app.root.findByName("Acc_Pos");

I have no idea whats going on and unfortunately because Im working on an AR project I cant you the debug tools to inspect the graph. I actually attached a model to the ‘app_pos’ too which is clearly visible in the scene so I can only guess that when instantiating the template it has somehow changed the name and tag on its own?? Could that be possible?

Any other ideas of what might be happening?

Without a repro, it’s going to be tough. If it’s an AR project, if you have an Android device you can use Chromes remote device debug tools and examine the graph manually.

What I would start off with is doing this in isolation on a new project. Copy and paste the template asset to that project and try doing the same code as before to see if you can reproduce it.

The other thing I would check is after instantiating the template, are you adding it the scene (addChild to another entity in the scene)?

How do I examine the graph in Chrome://inspect? I can see my debug message clearly enough but cant figure out how to examine the graph.

The project is already very small but yes, when instatiantiating it I am adding it as a child to another entity in the scene, but findByName or findByTag should find it all all the same no?

Cheers

Type the following console

var _app = pc.Application.getApplication();

Now you can access the root via:

_app.root

Unless you add an entity to the scene, findByName or findByTag will not find it because it’s not a child of app.root.

If it’s small, try reproducing this issue without needing AR which will make troubleshooting and debugging easier.

I reprod without AR to check the graph and the Acc_Pos does seem to exist so why would this.app.root.findByName(“Acc_Pos”); not work?

image

image

The issue here is scope. The error is saying that this.app is undefined as it’s trying to access the root property.

How is show_accesory function being called?

The show_accesory function (inside of AccesoryToggleButton) is being fired from another script using:

this.app.fire('collapseToggle:show_acc');

then in AccesoryToggleButton script I have…

this.app.on('collapseToggle:show_acc',this.show_accesory);

You are missing the scope parameter:

this.app.on('collapseToggle:show_acc', this.show_accesory, this);

See API here:
https://developer.playcanvas.com/en/api/pc.EventHandler.html#on

1 Like

Arrgh!!!

Thanks