I’m actually not quite sure what my problem is exactly, but I believe that the if statement inside the for loop in my HUD Script isn’t running.
**BUILDING SCRIPT:**
var playerBuilding : boolean = false;
var cleaningStart : boolean = false;
var cleaningCounter : int = 3;
function Update()
{
if(cleaningCounter <= 0)
playerBuilding = true;
}
function OnGUI()
{
if (selected && !playerBuilding)
{
if (GUI.Button (Rect(0, 100, 200, 100), "Clean Area"))
{
cleaningStart = true;
selected = false;
}
}
}
**HUD SCRIPT:**
function OnGUI()
{
if (GUI.Button (endDayRect, "End Day"))
{
for (var building : GameObject in buildingManager)
{
building.GetComponent("BuildingScript").selected = false;
if (building.GetComponent("BuildingScript").cleaningStart)
{
building.GetComponent("BuildingScript").cleaningCounter -= 1;
}
}
}
}
So in my game, there are a bunch of buildings on a map that all have the Building Script individually attached to them, and the player can click on them to select them. By default all buildings are set as !playerBuilding. The player is supposed to be able to change playerBuilding to true if they select “Clean Building” and pass a few in-game days by clicking “End Day”.
If the player selects a building that is not a playerBuilding, then a GUI button (“Clean Area”) Appears. If the button is clicked, it sets cleaningStart to true, and then it goes to the HUD Script.
In the HUD Script is another GUI button (“End Day”). Each building in the game is stored in buildingManager (a GameObject array), and I have a for loop to access each individual building in that list. The part in the for loop that makes the selected = false used to work before I added the if statement afterward, but now both don’t seem to be running. It is supposed to check if cleaningStart = true, and if so then each time “End Day” is clicked cleaningCounter should decrease by 1. Once the counter reaches 0 or less, playerBuilding should change to true (Building Script).
The problem is that in the inspector I can see that the cleaningCounter doesn’t ever go down on any building when I click “End Day”, even though cleaningStart = true. Also when I click “End Day” it doesn’t set selected to false.
I’m sorry if my question as well as my coding is a bit sloppy, but I was trying to keep it as simple as I could. I don’t know if my problem is using something wrong, or if I’m just overlooking something. If you need more explanation, or more of my code just ask.
Thanks in advance.
I'm sorry i code in javaScript so maybe I'm wrong, but in JavaScript the correct syntax for a for loop is for(variable ; when to end for loop (for instance i < 10) ; what to do to the variable (for instance i + 1) ) it looks like you're using collins instead of semi-collins and you only have 1 collin. Again this is coming from someone who uses JAVASCRIPT I am probably wrong
– rednax20@rednax20, the loop will work because because it is a foreach loop. var numberSet: int; ... //The usual for-loop for( i=0; i<numberSet.length; i++) { var number : int = numberSet*;* ... } //The foreach loop for( var number:int in numberSet ) { ... }
– Chronos-L