Let’s go back to your script. Thinking critically and following the logic of your code is very important to avoiding frustration. I’m going to use the version posted:
PlayerAttachments.prototype.initialize = function() {
this.weapon = this.current;
var weapon = current;
var current = this.entity.findByName('m4');
this.rhand = this.entity.findByName('mixamorig1:RightHand');
this.scaleOffset = 100;
//reparentLockTransform (this.current, this.rhand, this.scaleOffset);
reparentLockTransform( this.weapon, this.rhand, this.scaleOffset);
};
This is what happens when your script is initialized:
- You create a ‘variable’ (actually called a prototype, but for this case and for your understanding it’s the same) called
this.weapon
and make it equal to the currently undefinedthis.current
- You create a
var
called weapon and make it equal to the currently undefinedthis.current
- You create a
var
called current and make it equal to a search for a child entity called ‘mp4’ - You create a ‘variable’ called
this.rhand
and make it equal to a search for a child entity called ‘mixamorig1:RightHand’ - You create a ‘variable’ called
this.scaleOffset
and make it equal to 100 - You call the function
reparentLockTransform()
and pass the argumentsthis.weapon
,this.rhand
, andthis.scaleOffset
If you follow along these steps, hopefully you will realize that this.weapon
is failing because this.current
was never defined which means that this.weapon
is also undefined.
If you define this.current
properly at the top of the function (like you define var current
) it will work when you pass it along to reparentLockTransform()