#Help with Dropdowns, Events, and Listeners

1 messages · Page 1 of 1 (latest)

spring dock
#

ConnectCastesToGUI gets called once in Start. I'm getting IndexOutOfRangeException when ChangeColor triggers (ie, when the dropdown has its value changed). It thinks the casteIndex is 3?

public void ConnectCastesToGUI()
{
  for (int i = 0; i < Castes.Length; i++)
  {
    Debug.Log("Connecting Caste " + i + " to ColorDropdown " + i);
                       MainGUIPanel.Instance.ColorDropdowns[i].onValueChanged.AddListener(delegate{ChangeColor(i);});
   }
}

public void ChangeColor(int casteIndex)
  {
    Debug.Log("Attempting to change the color of Caste " + casteIndex + " to ");
    int colorIndex = MainGUIPanel.Instance.ColorDropdowns[casteIndex].value;
    Color color = PaletteManager.Colors[colorIndex];
     for (int j = 0; j < AntCountByCaste[casteIndex]; j++)
     {
       Ant ant = AntsByCaste[casteIndex][j];
       ant.Transform.GetChild(0).GetComponent<Renderer>().material.color = color;
     }  
    }
weak star
spring dock
#

Yes, no, yes, yes, yes

#

PaletteManager has like 5 colors. All the caste stuff has 3 elements rn.

#

Which is the problem. It seems i is 3, when it can't be higher than 2.

#

But I can't figure why it's 3.

#

It has to do with this listener event stuff which I don't understand.

weak star
#

wait wait
I think I know what's going on, delegates are a pain
can you put a debug in ChangeColor and tell me if casteIndex = Castes.Length? you can pass it as a second argument if its in a different script

spring dock
#

I have a debug, and yes, for my current setup they are equal. I can run some tests by changing the size of Castes to see if that's why it's 3.

#

Driving me bonkers this UI stuff

#

Pass what as a second argument?

#

I guess I should check I'm not calling ChangeColor anywhere else, but I don't think I am

weak star
#

if casteIndex == Castes.Length that means it gets passed as reference
I had this problem before and it is incredibly maddening, I can't quite remember how to fix the issue, it was a long time ago
but I think the issue is that the variable i gets passed along as a reference to ChangeColor function, not as value
I can't remember how to fix it but it was super silly

spring dock
#

Oh I see now

weak star
#

try
int k = i;
MainGUIPanel.Instance.ColorDropdowns[i].onValueChanged.AddListener(delegate{ChangeColor(k);});

spring dock
#

ah ofc

weak star
#

I think that worked? I remember it being just so stupid and unintuitive, I thought I dreamed it up tbh

spring dock
#

God thats wacky LOL

#

But it makes sense

weak star
#

...if it works, I can't promise, it was a long time ago

#

do tell me if it works

spring dock
#

Lemme try

weak star
#

the point is k being declared inside the for loop

spring dock
#

yeah yeah

#

my god

#

it works

weak star
#

lmao

spring dock
#

DOPE DOPE DOPE DOPE DOPE

weak star
#

that's delegates for ya

spring dock
#

i've been trying to get this to work all day

#

i ought to figure out how to make a gif

#

so you can reap the benefits visually

weak star
#

tbh that's why I'm staying away from them
unity actions are pretty cool and not... THIS

#

slower, but they make sense at least

spring dock
#

well originally I had had (v) => ChangeColor(i); in the listener

#

Wonder if that'd work now

weak star
weak star
#

drove me crazy