Here is a screen capture of the scene before we run it.
So you can see that the Sum Boxes are in fact prefabs that I instantiate at runtime. The Sum Box prefab has two children … a text box called SumBoxLabel that gets the number applied to it, and a red point light.
I have a script called PuzzleSetup that initializes all of the answers in the grid and displays the sums. I also have a script called SumCheck, which is where I am having the problem. It is within SumCheck that I wish to change the box’s color, but I cannot seem to reference the game object correctly.
Here is Puzzle Setup. I warn you it is long and ugly.
public class PuzzleSetup : MonoBehaviour
{
public bool used1 = false;
public bool used2 = false;
public bool used3 = false;
public bool used4 = false;
public bool used5 = false;
public bool used6 = false;
public bool used7 = false;
public bool used8 = false;
public bool used9 = false;
public bool used10 = false;
public int location1;
public int location2;
public int location3;
public int location4;
public int location5;
public int location6;
public int location7;
public int location8;
public int location9;
public int location10;
public int dummy1 = 0; // Holds the random location until we can check it
public int counter = 0; // Keeps track of which number we are putting into the location
public GameObject HexPrefab;
public GameObject SumBoxPrefab;
public int long0Sum;
public int long60Sum;
public int long300Sum;
public int med0Sum;
public int med60Sum;
public int med300Sum;
public int short0Sum;
public int short60Sum;
public int short300Sum;
// Solution initialization
void Start()
{
while (counter < 10)
{
counter ++;
dummy1 = Random.Range(1, 11); // Select a hex location for the current number
// Debug.Log("Random number: " + dummy1);
// Counter represents the numbers 1 through 9, and then the zero as the final number. The dummy1
// variable is randomly selecting a location in the pyramid for the current counter number to reside.
// The variables called used1 thru used10 will indicate that the selected pyramid location is already
// filled, causing the counter to not increment, effectively selecting a new location for that number.
switch (dummy1)
{
case 1:
if (used1 == true)
{
counter -= 1;
}
else
{
used1 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(240.918f, 1.48f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location1 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location1.ToString();
}
else
{
location1 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 2:
if (used2 == true)
{
counter -= 1;
}
else
{
used2 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(240.088f, 2.96f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location2 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location2.ToString();
}
else
{
location2 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 3:
if (used3 == true)
{
counter -= 1;
}
else
{
used3 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(241.8f, 2.96f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location3 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location3.ToString();
}
else
{
location3 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 4:
if (used4 == true)
{
counter -= 1;
}
else
{
used4 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(239.078f, 4.42f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location4 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location4.ToString();
}
else
{
location4 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 5:
if (used5 == true)
{
counter -= 1;
}
else
{
used5 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(240.918f, 4.42f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location5 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location5.ToString();
}
else
{
location5 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 6:
if (used6 == true)
{
counter -= 1;
}
else
{
used6 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(242.758f, 4.42f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location6 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location6.ToString();
}
else
{
location6 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 7:
if (used7 == true)
{
counter -= 1;
}
else
{
used7 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(238.376f, 6.02f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location7 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location7.ToString();
}
else
{
location7 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 8:
if (used8 == true)
{
counter -= 1;
}
else
{
used8 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(240.088f, 6.02f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location8 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location8.ToString();
}
else
{
location8 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 9:
if (used9 == true)
{
counter -= 1;
}
else
{
used9 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(241.8f, 6.02f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location9 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location9.ToString();
}
else
{
location9 = counter;
hexGO.tag = counter.ToString();
}
}
break;
case 10:
if (used10 == true)
{
counter -= 1;
}
else
{
used10 = true;
GameObject hexGO = (GameObject)Instantiate(
HexPrefab,
new Vector3(243.512f, 6.02f, 186.21f),
Quaternion.Euler(180, 0, 0),
this.transform
);
hexGO.name = "Hex" + dummy1;
if (counter == 10)
{
location10 = 0;
hexGO.GetComponentInChildren<TextMesh>().text = "0";
hexGO.tag = location10.ToString();
}
else
{
location10 = counter;
hexGO.tag = counter.ToString();
}
}
break;
}
}
//Debug.Log(location1 + " is in Location 1");
//Debug.Log(location2 + " is in Location 2");
//Debug.Log(location3 + " is in Location 3");
//Debug.Log(location4 + " is in Location 4");
//Debug.Log(location5 + " is in Location 5");
//Debug.Log(location6 + " is in Location 6");
//Debug.Log(location7 + " is in Location 7");
//Debug.Log(location8 + " is in Location 8");
//Debug.Log(location9 + " is in Location 9");
//Debug.Log(location10 + " is in Location 10");
long0Sum = location7 + location8 + location9 + location10;
GameObject sumGO1 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(237.22f, 7.07f, 186.05f),
Quaternion.Euler(180, 180, 0),
this.transform
);
sumGO1.name = "long0Sum";
sumGO1.tag = long0Sum.ToString();
sumGO1.GetComponentInChildren<TextMesh>().text = long0Sum.ToString();
med0Sum = location4 + location5 + location6;
GameObject sumGO2 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(237.97f, 5.6f, 186.05f),
Quaternion.Euler(180, 180, 0),
this.transform
);
sumGO2.name = "med0Sum";
sumGO2.tag = med0Sum.ToString();
sumGO2.GetComponentInChildren<TextMesh>().text = med0Sum.ToString();
short0Sum = location2 + location3;
GameObject sumGO3 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(238.72f, 4.13f, 186.05f),
Quaternion.Euler(180, 180, 0),
this.transform
);
sumGO3.name = "short0Sum";
sumGO3.tag = short0Sum.ToString();
sumGO3.GetComponentInChildren<TextMesh>().text = short0Sum.ToString();
long60Sum = location1 + location3 + location6 + location10;
GameObject sumGO4 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(245.80f, 9.03f, 186.05f),
Quaternion.Euler(180, 180, 60),
this.transform
);
sumGO4.name = "long60Sum";
sumGO4.tag = long60Sum.ToString();
sumGO4.GetComponentInChildren<TextMesh>().text = long60Sum.ToString();
med60Sum = location2 + location5 + location9;
GameObject sumGO5 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(244.05f, 9.03f, 186.05f),
Quaternion.Euler(180, 180, 60),
this.transform
);
sumGO5.name = "med60Sum";
sumGO5.tag = med60Sum.ToString();
sumGO5.GetComponentInChildren<TextMesh>().text = med60Sum.ToString();
short60Sum = location4 + location8;
GameObject sumGO6 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(242.30f, 9.03f, 186.05f),
Quaternion.Euler(180, 180, 60),
this.transform
);
sumGO6.name = "short60Sum";
sumGO6.tag = short60Sum.ToString();
sumGO6.GetComponentInChildren<TextMesh>().text = short60Sum.ToString();
long300Sum = location1 + location2 + location4 + location7;
GameObject sumGO7 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(242.96f, 1.0f, 186.05f),
Quaternion.Euler(180, 180, 300),
this.transform
);
sumGO7.name = "long300Sum";
sumGO7.tag = long300Sum.ToString();
sumGO7.GetComponentInChildren<TextMesh>().text = long300Sum.ToString();
med300Sum = location3 + location5 + location8;
GameObject sumGO8 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(243.95f, 2.55f, 186.05f),
Quaternion.Euler(180, 180, 300),
this.transform
);
sumGO8.name = "med300Sum";
sumGO8.tag = med300Sum.ToString();
sumGO8.GetComponentInChildren<TextMesh>().text = med300Sum.ToString();
short300Sum = location6 + location9;
GameObject sumGO9 = (GameObject)Instantiate(
SumBoxPrefab,
new Vector3(244.92f, 4.1f, 186.05f),
Quaternion.Euler(180, 180, 300),
this.transform
);
sumGO9.name = "short300Sum";
sumGO9.tag = short300Sum.ToString();
sumGO9.GetComponentInChildren<TextMesh>().text = short300Sum.ToString();
}
}
When you see me name something like short60Sum, that is the sum box on the short bar rotated at 60 degrees.