[SOLVED] Function is not defined

Hello,
I am having a small issue that has been plaguing me for too long. I have this code below:

var Gamemanager = pc.createScript('gamemanager');

var shuffleObjects;
var chosenObject;
var cloneMaterial;
var testBool;

// initialize code called once per entity
Gamemanager.prototype.initialize = function() {
    shuffleObjects = this.app.root.findByTag("SO");
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onSelect, this);
    this.timer = 0;
    this.isTimerActive = false;
    this.testBool = false;
};

Gamemanager.prototype.ColorChange = function(event){
    chosenObject = shuffleObjects[Math.floor(Math.random() * shuffleObjects.length)];
    cloneMaterial = chosenObject.model.meshInstances[0].material.clone();
    
    chosenObject.model.meshInstances[0].material = cloneMaterial;
    cloneMaterial.diffuse.set(1,1,0,1);
    cloneMaterial.update();
    this.isTimerActive = true;
};

// update code called every frame
Gamemanager.prototype.update = function(dt) {
    
    if(!this.isTimerActive)
        return;

    this.timer += dt;

    if (this.timer > 2) {
        cloneMaterial.diffuse.set(1,1,1,1);
        cloneMaterial.update();

        // Reset the timer
        this.timer = 0;
        this.isTimerActive = false;
        Gamemanager.MoveObjects();
    } 
};

Gamemanager.prototype.MoveObjects = function(){
    console.log("Im here");
};

When I launch this project, I get the error that MoveObjects is undefined almost as if it doesn’t recognize it as a function. Not sure whats wrong here at all. Any help would be appreciated.

Hi @aperez440,

Out of curiosity, does using this.MoveObjects(); instead of Gamemanager.MoveObjects(); in your update function work for you?

3 Likes

Welp, that worked perfectly haha. What made you think of that? Why does this work an not calling the reference to the script?

It comes down to scope mostly. In this case, since you already defined Gamemanager as a pc script at the top of your file You then entered into the scope of the Gamemanager object when you entered your update function (eg. Gamemanager.prototype.update).

So when you want to call another js prototype within that scope (Gamemanager.prototype.MoveObjects), you just need to use this

2 Likes

Awesome. thanks for letting me know. Your help has been very valuable.

1 Like