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 Muzz 1 · Apr 22, 2011 at 07:58 PM · buildingsswitch-case

Where am I going wrong (switch statement)

What's wrong with my code? It just keeps on generating buildings, doesn't stop.

var building1 : GameObject; var building2 : GameObject; var building3 : GameObject; var randomIndent = 0; var randomIndentLow = 3; var randomIndentHigh = -3; var numberOfBuildings = 4; var buildingNumber = 0; var zchange = 10;

function Update () { if (numberOfBuildings > 0); buildingNumber = Random.Range (1,3); randomIndent = Random.Range (randomIndentLow, randomIndentHigh);

switch (buildingNumber) { case (1) : Instantiate (building1, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10; case (2) : Instantiate (building2, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10; case (3) : Instantiate (building3, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10;

} }

Thanks!

Comment
Add comment
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

3 Replies

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

Answer by flaviusxvii · Apr 22, 2011 at 08:09 PM

break; at the end of each case!

Comment
Add comment · Show 3 · 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
avatar image Muzz 1 · Apr 22, 2011 at 08:24 PM 0
Share

I feel so stupid...how did I miss that?

avatar image Muzz 1 · Apr 22, 2011 at 08:32 PM 0
Share

Actually, I tried that and it still doesn't work.

avatar image flaviusxvii · Apr 22, 2011 at 08:53 PM 0
Share

Well, it WAS a problem. Now print the value of numberOfBuildings and make sure it is decreasing over time.

avatar image
0

Answer by burgunfaust · Apr 23, 2011 at 12:08 AM

Your problem is that you need to braces for your if statment.

if (numberOfBuildings > 0)
{
blah...blah....blah
}

Comment
Add comment · Show 6 · 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
avatar image FLASHDENMARK · Apr 23, 2011 at 08:35 AM 0
Share

Nope, you dont allways have to do that.

avatar image burgunfaust · Apr 23, 2011 at 01:00 PM 0
Share

But in this case it breaks the script with out it. Put them in and it works. The only time I know where you don't need it is if there is only 1 thing to execute, but this script needs more than that. That -1 is bull, because It works now.

avatar image Muzz 1 · Apr 23, 2011 at 05:07 PM 0
Share

I've got braces...look the line below.

avatar image burgunfaust · Apr 24, 2011 at 12:03 AM 0
Share

Not in your original code up top you don't. You have one for the Update and the switch but not the if.

function Update () { if (numberOfBuildings > 0); buildingNumber = Random.Range (1,3); randomIndent = Random.Range (randomIndentLow, randomIndentHigh);

avatar image Seregon · Apr 24, 2011 at 10:57 PM 0
Share

Actually burgunfaust has it right, his code change along with the added breaks fixes the original code, though I explained it better later, this is the correct solution. +1

Show more comments
avatar image
0

Answer by Seregon · Apr 23, 2011 at 09:11 AM

Actually, the problem is the if statement, more specifically the semi-colon:

if (numberOfBuildings > 0);

which is interpreted as

if (numberOfBuildings > 0)
; // ie - do nothing.

The rest of the code is always executed, as it's outside the if statement. I also agree that it needs braces enclosing the logic after the if statement, unless js doesn't require this?

Fixed code (including break;s):

var building1 : GameObject; var building2 : GameObject; var building3 : GameObject; var randomIndent = 0; var randomIndentLow = 3; var randomIndentHigh = -3; var numberOfBuildings = 4; var buildingNumber = 0; var zchange = 10;

function Update () { if (numberOfBuildings > 0) { buildingNumber = Random.Range (1,3); randomIndent = Random.Range (randomIndentLow, randomIndentHigh);

switch (buildingNumber) { case (1) : Instantiate (building1, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10; break; case (2) : Instantiate (building2, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10; break; case (3) : Instantiate (building3, transform.position + Vector3(randomIndent,0,zchange), transform.rotation); numberOfBuildings--; zchange +=10; break; } } }

Comment
Add comment · Show 3 · 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
avatar image burgunfaust · Apr 24, 2011 at 12:03 AM 0
Share

I already said that.

avatar image Seregon · Apr 24, 2011 at 10:23 PM 0
Share

Yes, and I agreed. You didn't mention the surplus semi-colon, though you did fix that in your code.

avatar image burgunfaust · Apr 25, 2011 at 12:51 AM 0
Share

Well if he had only one line to execute in his if statement, then the one semicolon would have been right.

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

No one has followed this question yet.

Related Questions

How do I enter buildings into my map? 1 Answer

Grid on Ship 1 Answer

Count Variable Acting Strange 1 Answer

Building a city 1 Answer

Why in the editor I see the scenes loading fine but in the build it's different ? 0 Answers


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