Dynamic name-referencing like in Actionscript?


i have various guiTextures which should be positioned on runtime. I would like to do it in a for-loop. In Flash/Actionscript i do it like this:

g0 = GameObject.Find("0");
g1 = GameObject.Find("1");
g2 = GameObject.Find("2");

gfx0 = g0.GetComponent(GUITexture);
gfx1 = g1.GetComponent(GUITexture);
gfx2 = g2.GetComponent(GUITexture);

gfx0.pixelInset = positionsGFX[0];
gfx1.pixelInset = positionsGFX[1];
gfx2.pixelInset = positionsGFX[2];

for(var i : int = 0; i < positionsGFX.length - 1; i++){
  ["gfx"+i].pixelInset = positionsGFX*;*
*<p>But that doensnt work in javascript/unityscript. Does anybody know how to do it?*
*Thanks for your help.</p>*

First of all, sorry I can't test every script, so the following are more like guide-lines

Using Extrakun's answer, and tuning it up, I think this is what you wanted:

// near-final script, but almost sure to work
var gfx;

for (var i : int = 0; i < positionsGFX.length - 1; i++) {
  for (var g : int = 0; g < 3; g++) {
    gfx = GameObject.Find(g).GetComponent(GUITexture);
    gfx.pixelInset = positionsGFX*;*
*<p>If it doesn't work, it's because you have to convert <strong>g</strong> to string. One way to do it would probably be "" + g.</p>*
*<p>Here's a middle step script, that works the same way, so maybe you can understand better how we got to this solution.</p>*
*// middle script - sure to work*
*var gfx0;*
*var gfx1;*
*var gfx2;*
*for (var i : int = 0; i < positionsGFX.length - 1; i++) {*
 *gfx0 = GameObject.Find("0").GetComponent(GUITexture);*
 *gfx1 = GameObject.Find("1").GetComponent(GUITexture);*
 *gfx2 = GameObject.Find("2").GetComponent(GUITexture);*
 _gfx0.pixelInset = positionsGFX*;*_
 <em>_gfx1.pixelInset = positionsGFX*;*_</em>
 <em><em>_gfx2.pixelInset = positionsGFX*;*_</em></em>
<em><em>_*<p>Can you see what was done from the middle script to the near-final? I hope it became obvious.</p>*_</em></em>
<em><em>_*<p>Now, your first script have 1 error and 1 "script warning":</p>*_</em></em>
<em><em>_*<p>The error is this line:</p>*_</em></em>
<em><em><em>_["gfx"+i].pixelInset = positionsGFX*;*_</em></em></em>
<em><em><em>_*<p>This is no javascript syntax and less yet in Unity. You just can't do that ["gfx"+i] to evaluate into a variable name, or anything. In most languages you'd want the function <strong>eval</strong> to do that, but I still don't know the equivalent in Unity. Maybe <a href="http://forum.unity3d.com/viewtopic.php?p=10380#10380" rel="nofollow">it still doesn't exist</a>. Anyway, <a href="http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea" rel="nofollow">eval should <strong>always</strong> be avoided</a>.</p>*_</em></em></em>
<em><em><em>_*<p>The "script warning" is almost everything else. But that was already fixed by Extrakun. Variables are used to avoid repeating stuff as a principle. If you are not going to use its value more than once, you should avoid creating them. There really was no need to make so many lines in this case.</p>*_</em></em></em>
<em><em><em>_*<p>You could also try this, which I'm not sure it will work, but I hope it should:</p>*_</em></em></em>
<em><em><em>_*// final resumed script - might have performance issues*_</em></em></em>
<em><em><em>_*for (var i : int = 0; i < positionsGFX.length - 1; i++) {*_</em></em></em>
 <em><em><em>_*for (var g : int = 0; g < 3; g++) {*_</em></em></em>
 <em><em><em><em>_GameObject.Find(g).GetComponent(GUITexture).pixelInset = positionsGFX*;*_</em></em></em></em>
<em><em><em><em>_*<p>Now, there's one issue with this... All those scripts above are "Finding" and "GettingComponent" for a number of <strong>i</strong> times, without needing.</p>*_</em></em></em></em>
<em><em><em><em>_*<p>So, the following is actually the best adaptation from your first example:</p>*_</em></em></em></em>
<em><em><em><em>_*// final script*_</em></em></em></em>
<em><em><em><em>_*private var gfx : Array;*_</em></em></em></em>
<em><em><em><em>_*var gfx = New Array(); // I think this can only go inside a function*_</em></em></em></em>
<em><em><em><em>_*// first set gfx so Find and GetComponent won't run i times, which might slow down performance*_</em></em></em></em>
<em><em><em><em>_*for (var g : int = 0; g < 3; g++) {*_</em></em></em></em>
<em><em><em><em>_*// now gfx is cached and we can just do the action for each "positionGFX"*_</em></em></em></em>
<em><em><em><em>_*for (var i : int = 0; i < positionsGFX.length - 1; i++) {*_</em></em></em></em>
 <em><em><em><em>_*for (var g : int = 0; g < 3; g++) {*_</em></em></em></em>
 <em><em><em><em><em>_gfx[g].pixelInset = positionsGFX*;*_</em></em></em></em></em>

You could store gfx0 to gfx2 in an array first. Or alternatively

var gfx;

for (var i:int =0; i < 3; i++)

  gfx = GameObject.Find("g" + i).GetComponent(GUITexture);

  // process gfx here


Basically, dynamically retrieve the game object, then get the component you wish to manipulate.