How to deal with 100 UI text elements displayed as a grid and sharing same event logic

Hi,
I’m a beginner in Unity and C#. I’ve spent hours and hours watching tutorials but I’m not sure what would be the best way to address following issue :

  • I will have on one single screen 100 UI “elements” displayed in ten lines of 10 columns. These elements will show no value in the begin. In my idea, each element could have two “properties” like LineName and ColumnNo.

  • when the user clicks on one of the elements, the system will calculate something based on the LineName and ColumnNo of this element (and other calculation parameters that I don’t need to describe here) and display the result in the element.

  • I would like to be able to “undo” the player’s choice. This means I need to know the last element that has been clicked after process. It would also be great if it was possible to set a backcolor to the selected element. After that, the player will click on a button to validate his choice, this would set the backcolor to default. The “Undo” option could be a button that retrieves the last clicked element and that sets the value back to null and the color to default.

  • I don’t want to use a grid because of display purposes.

My questions :

Q1 : what kind of UI element would best suit to this expected behaviour ?

  • Text ? I saw a video where the person added a component to the UI Text in order to change its display properties (change color on MouseMove, change value onMouseClick…)
  • Input Text (the user won’t input anything) ?
  • Buttons ? The Onclick event is available, the highlighted color is a standard behaviour, I know I can change the text shown by the button, etc… but it’s strange for me to use buttons for that purpose.
  • another UI element ?

Q2 : do I have to call the function that calculates the result on each of the 100 elements or is there a way to avoid this ? I dream of an “OnClick” event shared by all the 100 elements that would do the job based on the element’s “address” (LineName and ColumnNo).

Q3 : has someone seen something that looks like this in a sample project or a tutorial I could have a look at ?

I hope that at least one person has understood what I’m trying to do…

Thanks for any answer.

q1 : take a look at grid layout group and layout element.
q2 : yes you can create a logic like that, you may want to take a look at obj. oriented programming tips.
q3: sorry but no,

I found a workaround. I didn’t want to change the code on 100 buttons (calling the same function with the ButtonName as parameter). What I did is that each of the buttons now calls the same function without any parameter but this function finds in the EventSystem what was the last button that was clicked (string selectedButtonName = EventSystem.current.currentSelectedGameObject.name;)

I don’t know if that is the best solution but it works.

I didn’t find a solution in the OOP tips (I was looking for an option to bind the 100 buttons to an “Object Class” that would have hold the event-logic (OnClick event would then be set on the class and not on each of the buttons).