Can't get an entity to set position to another entity

I am working on a Harry Potter game, and I can’t get the

// initialize code called once per entity
Spells.prototype.initialize = function() {
var wandposition(this.wand.getlocalposition());
this.wand = this.entity.findByName(this.wandname);
this.camra = this.entity.findByName(this.camra);
};

The script is called Spells, the var is for finding the position. Later in code, I have this…

Spells.prototype.spellfire = function(dt){
this.entity.clone(
    this.clone.setposition(wandposition)
   
);
};

When I try this it says : Uncaught TypeError: Cannot read properties of undefined (reading ‘getlocalposition’)

TypeError: Cannot read properties of undefined (reading ‘getlocalposition’)
at https://launch.playcanvas.com/api/assets/files/Scripts/Spells.js?id=124993240&branchId=64347a85-ef63-4835-b74e-cf0c7f62d98f:26:30
Here is the full script.

var Spells = pc.createScript('spells'); 
Spells.attributes.add('spell', {
    type: 'string',
    array: false
});
Spells.attributes.add('spell_type', {
    description: 'DMG1, ESS2, CON3, FOR4, UILT5',
    type: 'number',
    default: 1
});
Spells.attributes.add('spell_type_spacific', {
    description: 'fire1, ',
    type: 'number',
    default: 1
});
Spells.attributes.add('wandname', {
    description: 'wand name',
    type: 'string',
    array: false
});
Spells.attributes.add('camra', {
    description: 'camra',
    type: 'string',
    array: false
});
var wandposition = this.wand.getlocalposition;
// initialize code called once per entity
Spells.prototype.initialize = function() {
var wandposition(this.wand.getlocalposition());
this.wand = this.entity.findByName(this.wandname);
this.camra = this.entity.findByName(this.camra);
};
Spells.prototype.update = function(){
if(this.app.keyboard.isPressed(pc.KEY_Z)){
    this.spellfire();
}
};

Spells.prototype.spellfire = function(dt){
this.entity.clone(
    this.clone.setposition(wandposition)
    

);
};
// swap method called for script hot-reloading
// inherit your script state here
// Spells.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// https://developer.playcanvas.com/en/user-manual/scripting/

I will appreciate any advice you have! :smile:

Hi @Codeknight999! I don’t understand what you’re trying to do, but what I can tell you is to watch the capital letters. For example, it is setPosition and getLocalPosition.

1 Like

Thanks a lot, this seems to work! :smiley:

1 Like

I am using a clone for this, like so…

this.other = this.entity.findByName
this.entity.clone();
this.clone.setPosition(this.other.getLocalPosition)
this.clone.rigidbody.applyForce(0, 0, 10);

Is this correct?

Line 1 is incomplete and line 3 is missing () after getLocalPosition and a ; sign at the end. Where do you define this.clone?

1 Like

I have that fixed in code but what do you mean by

You use this.clone but what is this.clone?

Do I have to do this…

this.entity.clone(spellclone)
this.spelclone.setPosition

?
I will test

If you don’t answer my question, I don’t know what you have to do.

this.entity.clone

is a clone of a cone that I am trying to shoot from another object.

Maybe you mean to do like below?

this.clone = this.entity.clone();

I mean this.clone is nothing by default.

Ok, the cloning works, but there are still problems with the

this.other = this.entity.findByName("entity");
this.clone.setPosition(this.other.getLocalPosition)

Has the entity with the script a child entity with the name ‘entity’?

Also here getLocalPosition should be getLocalPosition().

The entity is called Spells.
I will try the getLocalPostion().

Can getLocalPosition() be inside of a setLocalPosition() ?

Yes, that’s possible.

So is this code wrong?

this.entity.clone = this.entity.clone();
this.clone.setPosition(this.wand.getLocalPosition())
this.clone.rigidbody.applyForce(0, 0, 10);

Try this.

this.clone = this.entity.clone();
this.app.root.findByName('Root').addChild(this.clone);
this.clone.setPosition(this.wand.getLocalPosition());
this.clone.rigidbody.applyForce(0, 0, 10);

I tried that and it says:
[Spells.js?id=124993240&branchId=64347a85-ef63-4835-b74e-cf0c7f62d98f:40]: Cannot read properties of null (reading ‘getLocalPosition’)

TypeError: Cannot read properties of null (reading ‘getLocalPosition’)
at Spells.spellfire (https://launch.playcanvas.com/api/assets/files/Scripts/Spells.js?id=124993240&branchId=64347a85-ef63-4835-b74e-cf0c7f62d98f:40:34)
at Spells.update (https://launch.playcanvas.com/api/assets/files/Scripts/Spells.js?id=124993240&branchId=64347a85-ef63-4835-b74e-cf0c7f62d98f:33:10)
at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:91120:22)
at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:91154:15)
at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:91802:52)
at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:91820:11)
at ComponentSystemRegistry.fire (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:976:21)
at Application.update (https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:59565:19)
at https://code.playcanvas.com/playcanvas-1.61.3.dbg.js:60595:20

It means this.wand is undefined, so you need to check if you define it correctly.

I see you use this.entity.findByName to define it. This only works if it is a child entity. Maybe you need to use this.app.root.findByName?