0

In Unity, I have two arrays, one of strings:

var options = new string[] { "north", "east", "south", "west" }; 

and another of JSON objects named room, which I've setup per the MULTIPLE DATA(ARRAY JSON) solution given here. I won't list it here cos it's massive, but basically I can call room[1].north and get some text back successfully.

I want to add many more things to the options array, so I'm trying to write a foreach loop that will iterate over everything in the options array and perform the same methods on each, i.e.

foreach (string option in options) {
  GameObject optionObject = GameObject.Find (option);
  TextMesh optionTextMesh = optionObject.GetComponent<TextMesh> ();
  optionTextMesh.text = room[1].{some way to pass in "option" as the key};
}

Is it possible to interpolate a key on an object like this?

Community
  • 1
  • 1
sowasred2012
  • 695
  • 1
  • 7
  • 22
  • That depends entirely on the type of `room` and `room[1]`. Generally a JSON node class has a string indexer you can access to retrieve its members, so `room[1]["north"]`. Expect people to suggest reflection, but don't go that way. – CodeCaster Dec 04 '16 at 17:28
  • @CodeCaster ah, that could indicate I've gone the wrong route with the `room` array. I've created a custom class, Room, and a JSON helper class that wraps all the objects in the array so I can access those values (I'm new to C# and Unity, AFAIK Unity doesn't play with arrays of JSON objects). – sowasred2012 Dec 04 '16 at 17:37

0 Answers0