How to do a object like in javascript in C#

Hello everyone.

I’m learning C# and i have a question.
i have 4 public variable

    public int _nbLeft;
    public int _nbRight;
    public int _nbBack;
    public int _nbForward;

Basically i would like to create 4 button left if nbLeft = 4, 3 button right if nbRight = 3…

So my idea, instead of doing 4 for to do something close to the object like in Javascript?

like :

    var buttons= {};
    buttons._nbLeft= 1;
    buttons._nbRight= 2;
    buttons._nbBack= 2;
    buttons._nbForward= 2;
    
    for(var key in buttons){
          createButton(key,nb);
    }

-----------------------------------
this will give me :

createButton = function(key,nb){
      switch(_key){
          case _nbLeft: 
          // do button left...
          break; 
          etc...    
}

But i really don’t know in unity, the List can’t take an index and a key, the simple array is out of context…

Well, i don’t know ^^

Can’t someone show me a way to do it correctly.

thx :slight_smile:

P.S sorry for the tag… but it was one suggested… i can’t write C# cause apparently i create it…

There are many ways of writing of a code for your case. The example with comments is lower(write on CSharp):

 //And I see, you use switch-case, maybe create enum
 public enum myState { _nbLeft, _nbRight, _nbBack, _nbForward, _nbNothing};
 //_nbNothing: if is not show button

 //I see in code "var buttons". This is class or structure
 public class myButtons {
  //Create variable of enum and allocate it
  public myState[] stateBut = new myState[4];
 }
 //Create variable of class
 public myButton buttons = new myButtons();

 void Start() {
  //Initialization our enums button
  buttons[0] = myState._nbLeft;
  buttons[1] = myState._nbRight;
  buttons[2] = myState._nbBack;
  buttons[3] = myState._nbForward;
 }

 //In function OnGUI() draw buttons
 void OnGUI() {
  for(int i = 0; i < buttons.stateBut.Length; i++) {
   switch(buttons.stateBut*) {*

case myState._nbLeft:
//Do something for left
//For example, draw button in left and up
GUI.Button(new Rect(0, 0, 100, 100), “myLeftUp”);
break;
case myState._nbRight:
//Do something for right
break;
case myState._nbBack:
//Do something for back
break;
case myState._nbForward:
//Do something for forward
break;
}
}
}
I hope that it will help you.