Variable not being what it should be

Hi this is a little hard to explain so ill ive uploaded a video

to explain it better basicly when i instantiate something and i try and access its variable it takes from the latest instead of the one it should be

ive tried loading it from resources but it still does not work as intended. is there a way to make sure the script it separate from the rest of them?

try witout the line 45 in your SlotScript.

very interesting question. I wonder what does your SlotScript’s line 45 does? to me it seems like it assigns the variable to… itself?

anyways, i had similar issue in my code when I was trying to assign a function with different arguments to my objects’ delegates in the foreach() loop. I usually would end up with all the objects having the same delegate.

now my situation was distinct in one thing - the arguments of my delegates’ were the objects that foreach was iterating through. I mean - like this:

foreach(Slot slot in slots){
    GameObject slotPrefab = (GameObject)Instantiate(slotPrefab);
    slotPrefab.GetComponent<Button>().onClick.AddListener( () => SlotFunction(slot) );
}

this is a crude example but you got the point. so the solution was to introduce a temporary variable and feed THAT variable into the SlotFunction instead of what I had, so basically this:

foreach(Slot slot in slots){
    Slot tmpSlot = slot;
    GameObject slotPrefab = (GameObject)Instantiate(slotPrefab);
    slotPrefab.GetComponent<Button>().onClick.AddListener( () => SlotFunction(tmpSlot) );
}

I think this happened because the foreach loop iterates through the collection with a pointer (I’m not an expert in pointers, so I blame everything bad on them). I mean the delegate functions remembered the memory address of the same argument instead of different ones and after the loop was finished they all ended up with the same argument.

I hope this helps bro!
I’d love to see where this will go