Uncaught reference error: [something defined] is not defined

[Something defined] being colliders. Can someone explain to me why this is returning an uncaught reference error?

// initialize code called once per entity
RotateRing.prototype.initialize = function() 
{
    var colliders = this.app.root.findByTag('collider');
    
    for(i=0;i<colliders.length;i++)
    {
        colliders[i].rigidbody.enabled = false;
    }
};

RotateRing.prototype.spin = function()
{
    for(i=0;i<colliders.length;i++) //Error here
    {
        if(colliders[i].rigidbody)
        {
            colliders[i].rigidbody.enabled = true;
        }
    
        colliders[i].rigidbody.applyTorque(0,50,0);
    }
    
    this.entity.rotate(0,50,0);
};

Because colliders is not in the scope of function spin. You should make it a member variable in initialize.

Okay, but I’ve done this before in a previous project:

CheckRoll.prototype.initialize = function() 
{    
    var dice = this.app.root.findByTag('Dice');
    //More variables here
};

CheckRoll.prototype.check = function()
{
    //Makes sure each dice has stopped by comparing its current position
    //to its last position
    for(i=0;i<dice.length;i++)
    {
        if(stopped[i] === true)
        {
            continue;
        }
        var diePos = dice[i].getLocalPosition();
        if(lastPos[i] !== undefined)
        {
            if(this.toDecimal(diePos.x) === this.toDecimal(lastPos[i].x) && this.toDecimal(diePos.y) === this.toDecimal(lastPos[i].y) && this.toDecimal(diePos.z) === this.toDecimal(lastPos[i].z))
            {
                dice[i].stopped = true;
                this.stop = true;
            }
        }
        lastPos[i] = diePos.clone();
    }
    
    this.stopCheck();
};

There’s about three or four other places in this project where I call that dice variable outside of initialize without issue.

You are probably declaring dice somewhere globally elsewhere too. If you want to access variables in another function you need to declare them in a scope that contains the scope of the function or make them member variables e.g. this.colliders = ...

Here’s a link to the only script in the project I can find a declaration for dice.
https://playcanvas.com/editor/code/529780?tabs=10861152,10964296

I went through all other scripts and couldn’t find another pace I declare dice as something.

Wait I missed one…

That was it, I declared it here without realising.
https://playcanvas.com/editor/code/529780?tabs=10827801