Can't access method from another method?

I have trouble understanding what you can access and where.
I have Tests script with prototype method initialize where i want to bind method1 on click
I get the error Uncaught ReferenceError: method1 is not defined

how to define methods so i can access them this way

var Tests = pc.createScript('tests');

Tests.attributes.add("root", { type: 'entity' });
Tests.attributes.add("create", { type: 'entity' });

// initialize code called once per entity
Tests.prototype.initialize = function () {

    let app =;
    this.create.button.on('click', function (event) {



// add callback methods that will trigger on click
Tests.prototype.method1 = function () {

    console.log("button clicked");
    let root = this.root;

    for (let i = 0; i < 100; i++) {
        var templateAsset =;
        var instance = templateAsset.resource.instantiate();
        var pos = new pc.Vec3(0 + i * 10, 0, 0);

        let scriptInstance = instance.script.reelElementController.Test();

Tests.prototype.method2 = function () {

Hi @mirkoni,

In JavaScript you need to specify the context when declaring anonymous methods. You can either do so using bind(this) or PlayCanvas provides a convenience argument on events that can be used for this.

    this.create.button.on('click', function (event) {


    }, this);

Observe the change on the last line.

1 Like

ok, great it works.
i need to use this.method1(); though


1 Like