Programmatically retrieving dynamic variable information from another script.

How’s it going.

I have been working on a fairly decent sized mobile rpg game.

I’m currently at the stage where I am adding icons to all the items within the inventory of the player.

The players inventory consists of 30 slots, and I have a script called InventoryHandler that processes what goes where.

I also have a script called ItemHandler attached to a GameObject that is always in the scene.

The ItemHandler script has 4 variables for each item.

For Instance:

public string item_00001_name = "Logs";
public int item_00001_value = 74;
public Texture2D item_00001_icon;
public GameObject item_00001_model;

public string item_00002_name = "Ash";
public int item_00002_value = 1;
public Texture2D item_00002_icon;
public GameObject item_00002_model;

I have written the following script in the InventoryHandler script to try and pull the items texture from each item, but I am having difficulty trying to figure out how to get a specific variable name.

inventory_1_texture = game.GetComponent<ItemHandler> ().item_" + inventory_1_id + "_icon;

if (GUI.Button (new Rect (Screen.width / 1.399f, Screen.height / 2.44f, Screen.height / 12.96f, Screen.height / 14.02f), inventory_1_texture, inventoryStyle)) {

}

The entire code works except for this one part…

.item_" + inventory_1_id + "_icon;

If anyone could help me with calling a variables information by customising the call for the variable name, I would really appreciate it.

Thank you.

I did not try the code but on first looks you are making the first line execute like this (if it runs).
inventory_1_texture = game.GetComponent ().item_ + inventory_1_id + _icon;

This because your + is inside the " marks and there are spaces.
so my guess if this approach is good it should look like
inventory_1_texture = game.GetComponent ().item_ + “inventory_1_id” + _icon;
But I’m not entirely sure if this will run.

Good luck