Reference to script returning error no matter what method I try

Line 15 of this code always returns cannot read property of undefined error. Depending on how I set up the line, either by using this.dice.script, or just dice.script, the error reads cannot read property ‘script’ for this.dice, or cannot read property ‘applyForce’ for just dice.script. What am I doing wrong here?
https://playcanvas.com/editor/code/529780?tabs=10861152,10786903,10873926,10829078

You declare variables outside the initialize method of your script here:

this.rotateX = Math.floor(pc.math.random(minRot, maxRot) / 90) * 90;
this.rotateZ = Math.floor(pc.math.random(minRot, maxRot) / 90) * 90;

Move those inside the initialize method.

Tried that, still returns same errors.

Maybe you want to try and debug those? Basically if you do not learn how to use the debugger you’re going to have a very miserable time!

All the debugger is telling me, is that if the line is written this.dice, dice (which is declared in instantiate) is undefined and if its written dice.script, script is undefined. And yes, I am opening the debugger window (f12, or whatever key it is in the other browsers) and using a break point at the line in question.

If you look at https://developer.playcanvas.com/en/api/pc.Entity.html#findByTag you’ll notice that it returns an Array of Entities not a single Entity. Also you should write this.dice in your script everywhere and note that it will be an array if you assign the return value of findByTag to it.

The way I found that out was I opened the debugger and put a breakpoint in the initialize method of your CheckRoll script. After findByTag was executed I hovered over dice to see what value it had.

I intentionally set it up to do this, I have also tried using the same call using findByName instead and only referencing one of the objects, but the error still persists.

Putting a for loop in using the array seems to have removed the error. However something in the update function is interfering with the entities movement.