Recognizing N Buttons Click differently

Hi everyone,

I am building a game in which I have come across a situation where:

I have 150 Buttons in a scroll view.
I want to load different Levels on clicks of different Buttons.
For Instance;
On click of a button at 0 index => Level 1 will be loaded
On click of a button at 1 index => Level 2 will be loaded
and so on…

Solution I tried

  1. Naming buttons in Hierarchy like this
    Level Btns
  • Btn 1
  • Btn 2
  • Btn 3
    so on…
  1. Added script to each button that raises events with their respective names in parameters
  2. On event receive I split name on basis of space
  3. Parse index 1 to an integer to load the respective level

My Question:
Is this is the best way of doing it or is there any better way?

Thanks :slight_smile:

Given that all the buttons are likely to be the same, I would add the buttons via code using templates and adding the level number/value to a script on the button instance.

That script would set the text of the button to say ‘Level X’ where X is the level number.

Example based off the tutorial project: https://playcanvas.com/editor/scene/1241146

1 Like

Thanks, this is a better approach than mine.