- Home /
Incrementing a variable from a script in an Array
Hey im currently working on a 3d version of a board game, and currently trying to increment a variable thats in a script attached to a game object which is inside an array. Heres a chunk of the script so far.
function OnGUI(){
if(musterMenu == true && fortress > 0)
{
GUI.Box (Rect (Screen.width/2 - 440,Screen.height/2 - 60 ,900, 200), "Mustering");
GUI.Label(Rect(Screen.width/2 - 355,Screen.height/2 - 35,64,64),footmanTexture);
if(GUI.Button(Rect(Screen.width/2 - 420,Screen.height/2 +35, 200, 30),"Footman"))
{
Instantiate(1);
}
if(GUI.Button(Rect(Screen.width/2 - 210,Screen.height/2 +35, 200, 30),"Horseman"))
{
Instantiate(2);
}
if(GUI.Button(Rect(Screen.width/2 + 10 ,Screen.height/2 +35, 200, 30),"Ram"))
{
Instantiate(3);
}
if(seas.length > 0){
if(GUI.Button(Rect(Screen.width/2 + 220,Screen.height/2 +35, 200, 30),"Boat"))
{
boatPlacement = true;
}
}
if(GUI.Button(Rect(Screen.width/2 - 210,Screen.height/2 + 70, 200, 30),"Upgrade to Horseman"))
{
Instantiate(5);
}
if(GUI.Button(Rect(Screen.width/2 + 10,Screen.height/2 + 70, 200, 30),"Upgrade to Ram"))
{
Instantiate(6);
}
if(GUI.Button(Rect(Screen.width/2 + 340,Screen.height/2 + 90, 100, 40),"Return"))
{
musterMenu = false;
}
}
//GUI for Boat placement
if(boatPlacement == true)
{
musterMenu = false;
GUI.Box (Rect (Screen.width/2 - 340,Screen.height/2 - 60 ,700, 150), "Place Boat At");
if(seas.length == 1)
{
if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
{
location = seas[0].transform.position;
seas[2].GetComponent("Territory").unitCount++;
UnitPlacement(4);
boatPlacement = false;
}
}
if(seas.length == 2)
{
if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
{
location = seas[0].transform.position;
UnitPlacement(4);
boatPlacement = false;
}
if(GUI.Button(Rect(Screen.width/2 - 100,Screen.height/2 - 10, 200, 40),seas[1].tag))
{
location = seas[1].transform.position;
UnitPlacement(4);
boatPlacement = false;
}
}
if(seas.length == 3)
{
if(GUI.Button(Rect(Screen.width/2 - 320,Screen.height/2 - 10, 200, 40),seas[0].tag))
{
location = seas[0].transform.position;
UnitPlacement(4);
boatPlacement = false;
}
if(GUI.Button(Rect(Screen.width/2 - 100,Screen.height/2 - 10, 200, 40),seas[1].tag))
{
location = seas[1].transform.position;
UnitPlacement(4);
boatPlacement = false;
}
if(GUI.Button(Rect(Screen.width/2 + 120,Screen.height/2 - 10, 200, 40),seas[2].tag))
{
location = seas[2].transform.position;
UnitPlacement(4);
boatPlacement = false;
}
}
if(GUI.Button(Rect(Screen.width/2 -100,Screen.height/2 + 40, 100, 40),"Return"))
{
boatPlacement = false;
}
}
}
the variable im trying to access is in OnGUI() function, and is called unitCount. the script on the game objects are identical to this script, and the only thing i want to do is increment unitCount at seas[2].GetComponent("Territory").unitCount++; <-- this doesnt work so how can i get it to increment?
Answer by Bryson · Jul 25, 2012 at 08:11 AM
As it is now the line
seas[2].GetComponent("Territory").unitCount++;
is getting a copy of Territory's unitCount variable, without storing it, and incrementing that copy. You could actually set the value using something like
seas[2].GetComponent("Territory").unitCount = seas[2].GetComponent("Territory").unitCount++;
or you could write a simple function in Territory to increment unitCount and call it from OnGUI().
Even simpler is just seas[2].GetComponent("Territory").unitCount += 1
.
thanks the first one didnt work, but the second one did so thanks for the help
What is the type of unitCount - int? Is it a public variable? Also, why seas.get_Item(2) ins$$anonymous$$d of seas[2]? I'm not sure, but maybe this is returning a copy of the item, not a reference to the item itself.