I am trying to make a pool of elements, i have a template that is instantiated and put into pool
like this:
PoolManager.prototype.initialize = function () {
let app = this.app;
let template = app.assets.get(this.assetId);
for (let i = 0; i < this.itemCount; i++) {
let instance = template.resource.instantiate();
instance.script.reelElementController.Prepare();
instance.setLocalPosition(new pc.Vec3(0, 0, 0));
this.container.addChild(instance);
pool.push(instance);
// instance.enabled = false;
}
console.log(pool.length);
};
at the end console prints accurate items in the pool
every template has a script on it
the problem is when i want to access script it doesn’t let me
PoolManager.prototype.create = function () {
let item;
if (pool.length) {
item = pool.shift();
item.script.reelElementContoller.Test();
}
else {
let template = this.app.assets.get(this.assetId);
item = template.resource.instantiate();
item.script.Prepare();
}
this.Info();
return item;
};
so the question is how to access script on object that is in the pool?
this is common in unity it should be in playcanvas as well (sorry if this is beginner question, i am not expert in javascript)
i get this error
Uncaught TypeError: Cannot read properties of undefined (reading 'Test')
in initialize method it fills out the array with template objects and uses Prepare method on the script that is on the template, i can see it in the scene (it’s not disabled) but cant be accessed
Sorry it’s not easy to debug this out of context, seems more likely a JS reference issue. If you can provide a sample project that reproduces the issue, most likely it’s only a couple of minutes to debug it.