[SOLVED] Enabled error

I know I am probably doing something small wrong but I cant get this script to work:

var ColisionUi = pc.createScript('colisionUi');
this.count = 0;
ColisionUi.attributes.add('uiToTurnOn',{type:'entity',})
// initialize code called once per entity
ColisionUi.prototype.initialize = function() {
 this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.entity.collision.on('collisionend', this.onCollisionEnd, this);
};
ColisionUi.prototype.update = function(dt) {
if(count === 1){
        console.log('Start, this works');
 this.uiToTurnOn.enabled = true;
}else{
this.uiToTurnOn.enabled = false;
};
};


ColisionUi.prototype.onCollisionStart = function (result) {
    if (result.other.tags.has('player')) {
count = 1;
    }};


    ColisionUi.prototype.onCollisionend = function () {
count = 0;
    };
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// ColisionUi.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

I tried to use .bind(this) but it wouldn’t work on lines 13 or 15 and when I out it on 16 it still didn’t work.

Thanks for any help!

@Codeknight999 what seems to be the error? is it about this.count?

Hi @Codeknight999,

Just a quick glance, it looks like your capitalization for onCollisionEnd() on line 25 does not match the listener on line 7. Then, you’re declaring this.count outside of the script component. You would want to define it within initialize(). Inside of your update function you should be looking at this.count as opposed to count, because count is not defined in your update function.

I hope this is helpful.

Apologies for the confusion, here is the error I am getting:
Cannot set properties of undefined (setting ‘enabled’)

[ColisionUI.js?id=204141081&branchId=eac69c03-02df-4539-ad25-d27acd4e91a1:14]: Uncaught TypeError: Cannot set properties of undefined (setting ‘enabled’)

TypeError: Cannot set properties of undefined (setting ‘enabled’)
at https://launch.playcanvas.com/api/assets/files/ColisionUI.js?id=204141081&branchId=eac69c03-02df-4539-ad25-d27acd4e91a1:14:25
at ScriptComponent._scriptMethod (https://code.playcanvas.com/playcanvas-1.74.0.js:65909:18)
at ScriptComponent._onUpdate (https://code.playcanvas.com/playcanvas-1.74.0.js:65935:11)
at ScriptComponentSystem._callComponentMethod (https://code.playcanvas.com/playcanvas-1.74.0.js:66416:49)
at ScriptComponentSystem._onUpdate (https://code.playcanvas.com/playcanvas-1.74.0.js:66428:9)
at ComponentSystemRegistry.fire (https://code.playcanvas.com/playcanvas-1.74.0.js:859:18)
at AppBase.update (https://code.playcanvas.com/playcanvas-1.74.0.js:41149:17)
at https://code.playcanvas.com/playcanvas-1.74.0.js:41657:17

Also this is the editor link to the scene where this error is happening: PlayCanvas | HTML5 Game Engine

One of the entities with the script has no entity attached to the attribute.

I found and fixed that, but I am still getting the error.

Are you sure it’s the same error?

Can you show a screenshot of the error?

I don’t see any problems other than what the other users have already mentioned. I suggest to check this first.

On line 2 it looks like you create a global variable, that you can access in all scripts. You should do that like below.

var count = 0;

If you want to create a variable for this script only, you need to do this in the initialize function using this. Make sure you use also this when you use this variable elsewhere in your script.

Hi @Codeknight999,

This immediate error is because you defined update(dt) with .bind(this). Be carefull when dealing with different scopes in JS, especially if you’re understanding of them is not super strong. In this case, this does not refer back to the script component, but the global scope which is why uiToTurnOn and its enabled property return undefined.

Have a look here for some information about scopes in Javascript:

I have a feeling you will have other problems with your script, so you might want to take a look at the suggestions I made above, as well.

I hope this is helpful.

Where do you see this @eproasim?

I forked the project and inspected the relevant script. In the editor, .bind(this) is included in the update() definition.

I guess that means it can be done in another script?

I’m not sure I know what you mean. At the time of my fork, the script looked like this:

var ColisionUi = pc.createScript('colisionUi');
this.count = 0;
ColisionUi.attributes.add('uiToTurnOn',{type:'entity',})
// initialize code called once per entity
ColisionUi.prototype.initialize = function() {
 this.entity.collision.on('collisionstart', this.onCollisionStart, this);
    this.entity.collision.on('collisionend', this.onCollisionEnd, this);
};
ColisionUi.prototype.update = function(dt) {
if(count === 1){
        console.log('Start, this works');
 this.uiToTurnOn.enabled = true;
}else{
this.uiToTurnOn.enabled = false;
};
}.bind(this);


ColisionUi.prototype.onCollisionStart = function (result) {
    if (result.other.tags.has('player')) {
count = 1;
    }};


    ColisionUi.prototype.onCollisionend = function () {
count = 0;
    };
// uncomment the swap method to enable hot-reloading for this script
// update the method body to copy state from the old instance
// ColisionUi.prototype.swap = function(old) { };

// learn more about scripting here:
// https://developer.playcanvas.com/user-manual/scripting/

The problem being that binding script.update (on line 16) to the global scope loses its connection to the script component since it is being defined outside of the scope of the script object.

Ah, that wasn’t there on the moments I checked the script, so I guess that was a try to fix the problem.

I took that out.

Yep, and now count is undefined. Take a look at my previous reply for addressing that issue.

I didn’t know the thing about gobal variables, I thought global variables were something else, Ill check that link for more info when I get time. I put the this.count in the initialize function and fixed the script

It works now! kinda, I just have to work out the collision issues.

Thanks a lot for you help, I learned a lot

2 Likes