[SOLVED] Function is not defined

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;
    this.isTimerActive = true;

// update code called every frame
Gamemanager.prototype.update = function(dt) {

    this.timer += dt;

    if (this.timer > 2) {

        // Reset the timer
        this.timer = 0;
        this.isTimerActive = false;

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?


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


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

1 Like