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 30, 2011 at 03:18 PM · javascriptloopfor-loop

JS Loop question

I'm still trying to get this sorted...this is my first ever experience with editor scripting, and I'm grateful to everyone who's helped. Only question is : why isn't my for loop working?

class CityWizard extends ScriptableWizard {

var ground : GameObject; var numberOfBuildings : int = 20; var inititalBuildingNumber : int =20; var numberOfRows : int = 5; var building1 : GameObject; var building2 : GameObject; var building3 : GameObject; var MyPosition = Vector3(10, 0, 10); var buildingType = 4; var zchange = 10; var nothing : GameObject; var rowchange = 0; var rowchangeIncrease = 10;

@MenuItem("GameObject/City!")

static function City (){ ScriptableWizard.DisplayWizard("Title", CityWizard, "City", ""); }

function OnWizardCreate() {

 inititalBuildingNumber = numberOfBuildings;
 var newObj = Instantiate (ground, Vector3(0,-0.1,0), Quaternion.identity);
 newObj.transform.localScale.z = numberOfBuildings*40;
 newObj.transform.localScale.x = numberOfRows*100;       

 for(numberOfBuildings=20; numberOfBuildings <=0; numberOfBuildings--){

 buildingType = Random.Range (1,5);   

 print (buildingType);  

switch (buildingType) {

case (1) :
Instantiate (building1, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange);

break;

case (2) : Instantiate (building2, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange + 3);

break;

case (3) : Instantiate (building3, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange+3);

break;

case (4) : Instantiate (nothing, MyPosition, Quaternion.identity); MyPosition += Vector3 (0,0,zchange);

break;

 }

}

}

function OnWizardUpdate (){ helpString = ("Please assign a prefab to create!");

} }

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

2 Replies

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

Answer by Joshua · Apr 30, 2011 at 04:18 PM

It's because

for(numberOfBuildings=20; numberOfBuildings <=0; numberOfBuildings--)

This means start out at 20, increment by minus one as long as it's smaller then zero. It start's at 20 so it's not smaller then zero so it'll not even run once. I think it's a typo of >= 0 or you want something else but don't understand for loops.

G'd luck!

Comment
Add comment · Show 1 · 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 30, 2011 at 04:27 PM 0
Share

Thanks...that's awesome! Yeah, it was a typo.

avatar image
1

Answer by Kourosh · Apr 30, 2011 at 03:46 PM

Random.Range returns a float value. You need to have an integer instead in order to use switch case. Change that line to this:

buildinType = Mathf.Floor(Random.Range(1,6));
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 Muzz 1 · Apr 30, 2011 at 04:02 PM 0
Share

Doesn't help, sorry. You misspelt buildingType, but even with correct spelling it doesn't work. It doesn't even print the building type.

avatar image Muzz 1 · Apr 30, 2011 at 04:27 PM 0
Share

This may have been right as well, so I'll give you the upvote.

avatar image Joshua · Apr 30, 2011 at 07:01 PM 0
Share

a=Random.Range(b,c); doesn't return a float. It returns either a float or an int depending on weather a, b and c are all ints. If they are it returns an int between b [inclusive] and c [exlusive]. Else it's a float between b [inclusive] and c [inclusive]. Be very aware of the difference between the two.

avatar image Joshua · Apr 30, 2011 at 07:03 PM 0
Share

Also, his suggestion is just wrong since it will return the values 1,2,3,4 and 5 where you just want 1 to 4. You were using it correctly initially. Because you declare buildingType at the top as 4 and not 4.0 the compiler knows it's an int.

avatar image Joshua · Apr 30, 2011 at 09:12 PM 0
Share

I mean no offence by the way, $$anonymous$$ourosh! $$anonymous$$udo's for trying to help - just trying to counter the widespread misunderstanding about Random.Range :)

Show more comments

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

How to stop a for loop before it completes it's full cycle? 1 Answer

What's wrong with this Loop? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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