[SOLVED] Accessing members of another Entity

I’m try to create instances of Entities, and reference properties within those instances. Sort of like instancing prefabs in Unity, and then accessing members of the instance.

In my first Script (Script1.js), I create an Entity attribute like so:

Script1.attributes.add('otherEntity', {
    type: 'entity',
    title: 'Other Entity Prefab'

Then create a 2nd entity in PlayCanvas Editor, and then assign this 2nd Entity to the entity attribute slot of the original (1st) entity in the PlayCanvas GUI editor.

How can I then access members of otherEntity now, in Script1.js? If otherEntity has members created in initialize() like so:

Script2.prototype.initialize = function() {
    this.printCount = 0;
    this.maxPrints = 20;

And then in Script1.js, try to do something like this:
console.info(Script1.js: otherEntity.printCount = + this.otherEntity.printCount);

The outcome in the debug console is:
Script1.js: this.otherEntity.printCount = undefined

I’m missing something super-simple here I know :-D:thinking:

This super-simple project demonstrates what I’m trying (and failing) to do:

Here you trying to access the property of the entity rather than the script instance of Script2.

It would be this.otherEntity.script.script2.printCount

This post may help to grok how entities are structured: How to refer to different parts of the API while scripting

1 Like

That’s it! Easy peasy, thank you @yaustar!!!

Also, thanks for the link, that is very useful.