How to Set Instance Variable in Javascript?

I’m trying to assign an index variable to each Mushroom sprite in my scene, but this modifies the object variable for all Mushrooms. So I’m wondering how do I assign an “instance” variable to each individual Mushroom sprite in Javascript?

Try

for (const index in allMushrooms) {
allMushrooms[index].getVariables.get(“Index”).setNumber(index);
}

Did not work, still modifies the object variable unfortunately

What you want to achieve? Each object having an unique id variable?

I want to assign each Mushroom an index value to create a stagger effect.

I dont get why would not that code work? It should set the Index of each object to their order in the array.

Do you have any piece of code/event anywhere else changing their variables?

No I don’t modify the index elsewhere, I believe this doesn’t work because its setting the Object variable rather than the Instance variable, which means its assigning the same variable to all Mushroom sprite.

It’s working for me. I added the value to a text object and change the value with a cursor condition.

const Allmushrooms = runtimeScene.getObjects("Mushroom");

setInterval(function() {
for (let i = 0; i < Allmushrooms.length; i++){
    const mushroom=Allmushrooms[i]
    mushroom.getVariables().get("Index").setNumber(i);
}
},1000)

ScreenRecorderProject4_2