0

Can I get the field name from the parameter? I would like to call the method like CompareValues("points");

public void CompareValues(string field)
    {
    int value = Card.GetComponent<CardScript>().field;

Ruzihm
  • 19,749
  • 5
  • 36
  • 48
Tommi
  • 3
  • 2
  • 1
    Seems like an [XY problem](http://xyproblem.info/). Why are you trying to do this? – Ruzihm Nov 02 '20 at 16:35
  • I have about 10+ fields (Card game). Now I have to create multiple methods to get the values. I would like to have one method and pass an argument what field I would like to get. – Tommi Nov 03 '20 at 19:46
  • Alright. So, how do those methods get the name of the field to display? – Ruzihm Nov 03 '20 at 19:48
  • Thanks, button click. For every field there is a button also. – Tommi Nov 03 '20 at 20:55
  • Instead of using reflection, I would instead recommend to [bind listeners](https://stackoverflow.com/a/41392130/1092820) to the buttons. and then make `CompareValues` take getter and setter delegates as parameters, and use those inside of `CompareValues`. For instance, `public void CompareValues(Func getter, Action setter){ int value = getter();}` then when binding the liseners, create the getters & setters: `button1.onClick.AddListener(() => CompareValues(()=>Card.GetComponent().field, (int inValue) => Card.GetComponent().field = inValue));` – Ruzihm Nov 03 '20 at 21:46
  • Thanks - I need to dive into this. So many new concepts for me :) – Tommi Nov 04 '20 at 13:30

0 Answers0