# How do you name a variable using the value of another variable?

Currently I am a high school student attempting to make a video game. I have created my own club dedicated to it and this has stumped me.

I am trying to make “sectors” on the 2d plane by using Rects, but in trying to make the code a lot simpler I have hit a road block.

Here is the very rudimentary code (old)

``````#pragma strict
var guEq : Transform;
var plaPos : Transform;

function Update () {
var sec9vec : Vector2;
sec9vec.x = plaPos.position.x + 0;
sec9vec.y = plaPos.position.y + 0;
var sec6vec : Vector2;
sec6vec.x = plaPos.position.x + 1.1;
sec6vec.y = plaPos.position.y + .1;
var sec5vec : Vector2;
sec5vec.x = plaPos.position.x + 1.1;
sec5vec.y = plaPos.position.y + 2;
var sec4vec : Vector2;
sec4vec.x = plaPos.position.x + 0;
sec4vec.y = plaPos.position.y + 2;
var sec3vec : Vector2;
sec3vec.x = plaPos.position.x - 1.1;
sec3vec.y = plaPos.position.y +2;
var sec2vec : Vector2;
sec2vec.x = plaPos.position.x -1.1;
sec2vec.y = plaPos.position.y + .1;

var sec9 = Rect(Screen.width/3, Screen.height/3, Screen.width/3, Screen.height/3);
var sec6 = Rect(Screen.width/3 *2, Screen.height/3, Screen.width/3, Screen.height/3);
var sec5 = Rect(Screen.width/3 *2,Screen.height/3 *2,Screen.width/3, Screen.height/3);
var sec4 = Rect(Screen.width/3,Screen.height/3 *2,Screen.width/3,Screen.height);
var sec3 = Rect(0,Screen.height/3 *2,Screen.width/3,Screen.height/3);
var sec2 = Rect(0, Screen.height/3, Screen.width/3, Screen.height/3);

if(sec9.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec9vec, Time.deltaTime * 5);
}
if(sec6.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec6vec, Time.deltaTime * 5);
}
if(sec5.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec5vec, Time.deltaTime * 5);
}
if(sec4.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec4vec, Time.deltaTime * 5);
}
if(sec3.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec3vec, Time.deltaTime * 5);
}
if(sec2.Contains(Input.mousePosition)) {
guEq.position = Vector2.Lerp(guEq.position, sec2vec, Time.deltaTime * 5);
}

}
``````

And this is the new code that will be more accurate

``````#pragma strict

var sectors : float = 1;
//X-positions
var x1 = 0;
var x2 = Screen.width/6;
var x3 = Screen.width/6 * 2;
var x4 = Screen.width/6 * 3;
var x5 = Screen.width/6 * 4;
var x6 = Screen.width/6 * 5;
//Y-positions
var y1 = 0;
var y2 = Screen.height/6;
var y3 = Screen.height/6 * 2;
var y4 = Screen.height/6 * 3;
var y5 = Screen.height/6 * 4;
var y6 = Screen.height/6 * 5;
//Xmin
var Xmin;
//Ymin
var Ymin;

function Start () {
while(sectors<36) {
var sec(This is where I want to add the value of the variable of sectors) = Rect(Xmin,Ymin,x1,y1);
sectors += 1;
}

}

function Update () {

//Ymin
if(sectors>=1 && sectors<=6) {
Ymin = y1;

}
if(sectors>=7 && sectors<=12) {
Ymin = y2;

}
if(sectors>=13 && sectors<=18) {
Ymin = y3;

}
if(sectors>=19 && sectors<=24) {
Ymin = y4;

}
if(sectors>=25 && sectors<=30) {
Ymin = y5;

}
if(sectors>=30 && sectors<=36) {
Ymin = y6;
}
//Xmin
if(sectors==1 && sectors==7 && sectors==13 && sectors==19 && sectors==25 && sectors==31) {
Xmin = x1;
}
if(sectors==2 && sectors==8 && sectors==14 && sectors==20 && sectors==26 && sectors==32) {
Xmin = x2;
}
if(sectors==3 && sectors==9 && sectors==15 && sectors==21 && sectors==27 && sectors==33) {
Xmin = x3;
}
if(sectors==4 && sectors==10 && sectors==16 && sectors==22 && sectors==28 && sectors==34) {
Xmin = x4;
}
if(sectors==5 && sectors==11 && sectors==17 && sectors==23 && sectors==29 && sectors==35) {
Xmin = x5;
}
if(sectors==6 && sectors==12 && sectors==18 && sectors==24 && sectors==30 && sectors==36) {
Xmin = x6;
}
}
``````

The problem lies within the start function, and I would really appreciate the help. One last thing, I must ask you to add a short explanation on how the code works, I really need to understand how it works so I can compile a “cheat sheet” for my other club members.

Scripts in Unity are compiled, thus some typical web javascript tricks like forming the variable name with the value of another variable aren’t possible - usually this is done with arrays.

If you want to split the screen into 36 sectors, do something like this:

``````var secs: Rect[] = new Rect[36];

function Start(){
var w: float = Screen.width/6;
var h: float = Screen.height/6;
for (var n: int = 0; n < 36; n++){
var y = (n/6) * h;
var x = (n%6) * w;
secs[n] = Rect(x, y, w, h);
}
}
``````

There is no way to define identifiers (name variables) dynamically at runtime, because they exist at two different sides of the wall that is compilation. That’s putting it a little simple, so allow me to expand slightly. The source code you write is on a very high level of abstraction. It’s code made humanly readable - the CPU doesn’t work with characters or variable names or bracket-delimited blocks. Instead, it works with ‘machine code’, which is a huge series of 0’es and 1’es that represent the instructions. The compiler’s job is to turn your humanly readable source code into CPU readable binary machine code.

Thus, when compilation is done and your program runs, all names you define cease to exist and are known to the CPU only as hex valued memory addresses. That’s also why you can’t save some code in a string (or as text in a .txt) and then ask the CPU to dynamically execute that at some point in the program. It can’t, because that code hasn’t been compiled yet. It’s still in string format when the program runs. This is the main difference between a compiled language and an interpreted language. An interpreted language interprets and executes code on the fly instead of compiling it to machine code first. Examples of such are often found in web technologies, e.g. ASP, PHP, Python, Ruby and JavaScript (the web version, not the Unity version).

Returning to your concrete question here, I totally understand your desire to make that code leaner and cleaner. It’s got a lot of repetition going on. But instead of keeping those variables individual and with their own unique names, you should create an array and stick your calculations into the slots of the array. Then you access each set of x and y through their array index instead of variable names. It could look like this (C#):

``````// Vertical and horizontal number of sectors. Kept different so that you can have an unequal amount vertically and horizontally if you want
private int horizontalNumberOfSectors = 6;
private int verticalNumberOfSectors = 6;

// A double array - think of it as a 2D table. Can also be declared as sectorPositions[][]
private Vector2[,] sectorPositions = new Vector2[horizontalNumberOfSectors, verticalNumberOfSectors];

private void Start()
{
// Do this calculation once and save the result in a variable instead of repeating the calculation over and over again
float widthFraction = Screen.width / horizontalNumberOfSectors;
float heightFraction = Screen.height / verticalNumberOfSectors;

for (int i = 0; i < horizontalNumberOfSectors; i++) // Outer loop iterates horizontally
{
for (int j = 0; j < verticalNumberOfSectors; j++) // Inner loop iterates vertically
{
// Set x and y position. This is going to be at the CORNER of the section in screen space.
// If you want this to be the screen space middle of the sector, you need to add half the width and height of one sector
sectorPositions[i, j] = new Vector2(i * widthFraction, j * heightFraction);
}
}
}
``````

I apologize for this being in C# - I’m not a UnityScript programmer. Maybe someone else from this site can help you translate it. The code just sets up the array of screen space sector positions, preparing them for use in the Start method. It looks like that’s what you’re trying to do or need to do.

After this setup, you can access the individual Vector2’s by indexing into this array:

``````private void Update()
{
Vector2 theOneInTheLowerCorner = sectorPositions[0, 0];
Vector2 theOneInTheMiddle = sectorPositions[3, 3];
Vector2 theOneInTheUpperCorner = sectorPositions[6, 6];
}
``````

Again, I’m so sorry for this being C#. I just can’t vouch for its correctness if I translate to UnityScript by hand. I love the fact that you’re a bunch of tech-interested high schoolers in a club, and wish you all the best.