- Home /
For Loop Doesn't Appear to be running
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 ins$$anonymous$$d of semi-collins and you only have 1 collin.
Again this is co$$anonymous$$g from someone who uses JAVASCRIPT I am probably wrong
@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[i];
...
}
//The foreach loop
for( var number:int in numberSet ) {
...
}
@Saybejay, you should throw some Debug logs in your code so that you can see if your logic is working the way you expect.
if (GUI.Button (endDayRect, "End Day"))
{
Debug.Log("End Day clicked");
for (var building : GameObject in building$$anonymous$$anager)
{
Debug.Log("Building: " + building.name + ", selected to false");
building.GetComponent("BuildingScript").selected = false;
if (building.GetComponent("BuildingScript").cleaningStart)
{
Debug.Log("Cleaning Counter decremented");
building.GetComponent("BuildingScript").cleaningCounter -= 1;
}
else
{
Debug.Log("cleaningStart = false");
}
}
}
Answer by Saybejay · Apr 05, 2013 at 05:27 AM
Thanks for the comments people! I actually ended up rewriting my whole code almost from scratch, and now it works fine. I think what my problem was, was that my buildingManager variable that stored the buildings was in function Awake instead of function Start. I noticed that the buildingManager wasn't storing any buildings on play, so once I switched this it seemed to do the trick. So the code I posted, it seems didn't even end up addressing what my actual problem was.
You're right though whebert, I'll start putting in some more Debug.Log's, which I hope will help me avoid making such a careless mistake again..
Your answer
Follow this Question
Related Questions
What am I doing wrong with this bool? 3 Answers
Using if/else statements in GUI 1 Answer
Problem with for loop 0 Answers
Creation of a Pause and a Console Menu? 2 Answers
(For Loop) Converting Gui Button to GuiText & GuiTexture 1 Answer