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 25, 2011 at 08:30 AM · javascriptif

If statement not working

I've been writing a code, and I can't get my if statement to work at all. here is the code - what is wrong with it? The if statment not working is the bottom one.

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 position = Vector3 (10,0,10); var buildingType = 4; var zchange = 10; var nothing : GameObject; var rowchange = 0; var rowchangeIncrease = 10;

function Start (){

var newObj = Instantiate (ground, Vector3(0,-0.1,0), transform.rotation); newObj.transform.localScale.z = numberOfBuildings*20; newObj.transform.localScale.x = numberOfRows*40;

}

function Update () {

if (numberOfBuildings > 0) {

buildingType = Random.Range (1,5);

print (buildingType);

switch (buildingType) { case (1) : Instantiate (building1, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (2) : Instantiate (building2, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (3) : Instantiate (building3, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (4) : Instantiate (nothing, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

if (numberOfBuildings == 0 && numberOfRows > 0){ print ("next"); position == Vector3 (10,0,0); numberOfBuildings += inititalBuildingNumber; rowchange += rowchangeIncrease; numberOfRows--; } }

} }

Thanks.

Comment
Add comment · Show 2
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 25, 2011 at 08:54 AM 0
Share

None of the solutions even print next!

avatar image Muzz 1 · Apr 25, 2011 at 09:45 AM 0
Share

Just realised I made an error - > ins$$anonymous$$d of ==. However, the position still does not go back.

3 Replies

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

Answer by Scribe · Apr 25, 2011 at 01:35 PM

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;

function Start (){ inititalBuildingNumber = numberOfBuildings; var newObj = Instantiate (ground, Vector3(0,-0.1,0), transform.rotation); newObj.transform.localScale.z = numberOfBuildings*20; newObj.transform.localScale.x = numberOfRows*40;

}

function Update () { if (numberOfBuildings > 0) { buildingType = Random.Range (1,5); print (buildingType);

     switch (buildingType) {
         case (1) :
         Instantiate (building1, MyPosition, transform.rotation);
         MyPosition += Vector3 (0,0,zchange);
         numberOfBuildings--;
         break;

         case (2) :
         Instantiate (building2, MyPosition, transform.rotation);
         MyPosition += Vector3 (0,0,zchange);
         numberOfBuildings--;
         break;

         case (3) :
         Instantiate (building3, MyPosition, transform.rotation);
         MyPosition += Vector3 (0,0,zchange);
         numberOfBuildings--;
         break;

         case (4) :
         Instantiate (nothing, MyPosition, transform.rotation);
         MyPosition += Vector3 (0,0,zchange);
         numberOfBuildings--;
         break;
     }
 }
 else {
     if (numberOfRows > 0){
         print ("next");
         MyPosition += Vector3 (rowchangeIncrease,0,-(zchange*inititalBuildingNumber));
         numberOfBuildings += inititalBuildingNumber;
         rowchange += rowchangeIncrease;
         numberOfRows--;
     }
 }

}

I got it to work for me after I fiddled around for a bit and changed and reorganized some of your code

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 Scribe · Apr 25, 2011 at 01:38 PM 0
Share

seeing as it uses else you dont need to ask if numberOfBuildings is 0 as it works as long as its not more than 0 which you specified in the first if statement

avatar image Muzz 1 · Apr 25, 2011 at 02:45 PM 0
Share

That works brilliantly. Wow. Vote this up, everyone!

avatar image Scribe · Apr 25, 2011 at 04:50 PM 0
Share

haha thanks for voting up

avatar image
3

Answer by Alec-Slayden · Apr 25, 2011 at 08:46 AM

You have nested it in your switch statement. Just move it outside the next closing bracket.

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
avatar image
1

Answer by Uzquiano · Apr 25, 2011 at 08:46 AM

Hi,

Obviously, because it is inside the 'switch(buildingType)' so if it goes always to any case the 'switch' statement is over because of the 'break'

It should be something like this

switch (buildingType) { case (1) : Instantiate (building1, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (2) : Instantiate (building2, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (3) : Instantiate (building3, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break;

case (4) : Instantiate (nothing, position, transform.rotation); position += Vector3 (0,0,zchange); numberOfBuildings--; break; }//end switch

if (numberOfBuildings == 0 && numberOfRows == 0){ print ("next"); position == Vector3 (10,0,0); numberOfBuildings += inititalBuildingNumber; rowchange += rowchangeIncrease; numberOfRows--; }

Comment
Add comment · Show 2 · 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 25, 2011 at 08:53 AM 0
Share

This doesn't work either!

avatar image Uzquiano · Apr 25, 2011 at 09:17 AM 0
Share

So, your problem might be that numberOfRows is never 0 at that point...

I would suggest you to the a 'Debug.Log' just before the if() and see their values

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Javascript in Unity, Including iPhone API's (Objective C?) 2 Answers

How to toggle a key for a car to go forward or backward? 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