How do I access an instance variable in JavaScript?

I’m iterating through objects, and I need to set their instance variable called “ColorPhase” to zero. I saw someone suggest “Obj.Variable(VarName)”, but it’s not working for me. I’m currently using the following:

sprockets[i].Variable(ColorPhase) = 0;

I checked the documentation and couldn’t find anything about accessing instance variables from JavaScript.

I’d actually prefer to be able to set the instance variable in my events, but it’s not working for some reason. I’m using:

I have that set to trigger when a button is clicked, but it’s not taking effect. I have other events that trigger when that button is clicked, it’s just this one thing that won’t work. Is there something I’m missing about instance variables?

objects[i].getVariables().get(“ColorPhase”).setNumber(0);

1 Like

Thanks, I believe that’s working, but I’m still missing something. I think I need to call this behavior on each iteration:

I got this far:

sprockets[i].getBehavior("SpriteRecolorizer").

but I’m not sure how to proceed, or if that’s even correct so far.

objects[i].getBehavior(“SpriteRecolorizer”).recolorizer.resetSprite();

1 Like

You should not have to do all this, you’re probably making your life difficult.
Take a look at this page section to understand when to use JS or events:
https://gdevelop-wiki-git-js-in-extensions-gdevelop.vercel.app/gdevelop5/events/js-code/javascript-in-extensions/#use-the-power-of-both-events-and-javascript

I tried using events. I tried over and over, different places, everything I can think of, and I couldn’t get it to work with this extension. The Recolorizer extension is the only time I’ve had this problem.

What was the issue with the Recolorizer extension?

I couldn’t get the “reset color” event to work. I had it set to trigger when I tap a button, and the color would never reset.