#find button in list based on selected button

1 messages · Page 1 of 1 (latest)

remote fern
#

So now we dont have to fill up that channel

radiant ether
#

That's clever, it's been so much time since these were disabled, I don't remember to use them lmao

remote fern
#

Honestly I hated the addition of threads, but only because I was in servers who didn't need them at all

#

but for stuff like this it's good

#

Anyways

radiant ether
#

So, any other errors? You should have one I think

remote fern
#

None

radiant ether
#

So you edited EmployeeButtonClicked so it takes a param of type Button? Good

remote fern
#

So an employee object is a prefab, generated like:
Employee1:
|--- Name (Text)
|--- Design (Sprite)
|--- B_SelectEmployee (Button)

#

the B_SelectEmployee is what we're dealing with

radiant ether
#

Yup that's what I deduced from the original code you posted

remote fern
#

all employees are gathered into a list called NotHired

#

all buttons are gathered into a list called EmployeeButtons

#

which are generated right after another, so they're 1 - 1

#

so if I can get the index of the selected button in employeebuttons, I'll get the one in NotHired too

#

you probably already figured that out, but that's the goal

radiant ether
#

Okay, now show the EmployeeButtonClicked handler method, the magic will happen there

remote fern
#
public void EmployeeButtonClicked(Button button)
    {

    }```
#

nothing atm

#

I have a selectedEmployee int which will be the returned index

radiant ether
#

Okay good. So you've got the button that raised the event as a parameter, the button list as a class field, and the IndexOf method. You're all set

remote fern
#

RIght

#

Actually

#

Each button has a selected attribute right

#

we could do
for i = 0; i < EmployeeButtons.Count' i++
if EmployeeButtons[i].isSelected
return i

radiant ether
#

You don't need it. You have a reference to the clicked button (your parameter), and the button list.
Debug.Log(EmployeeButtons.IndexOf(button)); should print out the index of the sender in the console.

remote fern
#

Oh

#

Let's check that

#

Yep that did it

radiant ether
#

It's a shame that the overall Unity event system doesn't include that "sender pattern", and you have to do it yourself

remote fern
#

So then I would do
selectedEmployee = EmployeeButtons.IndexOf(button)

#

Yeah

radiant ether
#

Seems right to me

remote fern
#

This is a very UI-based game

#

given it's primarily about management

radiant ether
#

Okay, so if there is no more questions nor anything else to discuss, I'll let you archive this thread to clean up