Variable returning undefined in Update()


    start() {
        window.addEventListener('wheel', this.OnMouseWheelChanged, { passive: false });
    }

    OnMouseWheelChanged(event) {
        this.scrollDelta = 35;
        console.log(this.scrollDelta);
    }

    update() {
        console.log(this.scrollDelta);
    }```

Hi there! I set the number to 35 and console.log, and it comes out correctly. However, if I log the same variable in Update(), it comes out as undefined. Any ideas?
![image.png](https://cdn.discordapp.com/attachments/1039538346046787584/1039538346399105134/image.png?ex=65d4af07&is=65c23a07&hm=0fac6f29f22b45afbeb11d1c8a6090b19e74d3669a3f4e4c9a33e37abdd77eb5&)

[Original Post on Discord](https://discord.com/channels/717429793926283276/1039538346046787584)

*by user 95852307077406720*

its because you need to bind your method “OnMouseWheelChanged” to the class instance of your script. this is undefined in OnMouseWheelChanged

Change it to:

window.addEventListener('wheel', this.OnMouseWheelChanged.bind(this), { passive: false });

You can also use the value stored in this.context.input for the scrolldelta in update directly

Thank you, I apologise about these silly questions, this is a totally new paradigm to me haha

by user 95852307077406720

No problem

That’s not a silly question tho :sweat_smile: was very confusing / strange to me too when I first learned about it

For quite a long time actually until I worked more with all the js stuff.
E.g. when you write a function using arrow notation it auto-binds (e.g. () => { console.log(this) } vs function() { console.log(this) })

Or you can call a function with myFunction.call(this, firstArgument, secondArgument) which is the same as myFunction.bind(this)(firstArgument, secondArgument)