Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Saybejay · Apr 03, 2013 at 06:21 PM · guibooleanif-statementsfor-loop

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rednax20 · Apr 05, 2013 at 01:53 AM 0
Share

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

avatar image Chronos-L · Apr 05, 2013 at 02:06 AM 0
Share

@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 ) {
     ...
 }
avatar image whebert · Apr 05, 2013 at 02:30 AM 0
Share

@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");
          }
      }
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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..

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges