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.
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 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.
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’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.
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