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 JonJacob · Mar 06, 2013 at 10:51 AM · randomif-statementsif-else

if else statement ignored ??

I've been up for too long already and I need some fresh eyes to look at this simple snippet of code and see if I am doing anything too stupid here.

First off let me explain what I am trying to accomplish. I am making a simple temple run type endless running game and am just in the preliminary stages of figuring some mechanics out. What I am doing right now is moving cubes representing the ground you are running on to just past the screen view. Once the cube reaches this point a random number is called and if the number is 0 it will make the cube transparent and stop the physics on the cube so that the player can fall through it.

The problem I am having is that Unity will apparently ignore my if else logic preventing the creation of two transparent blocks at once.

This is a problem since my "player" can't jump two spaces at once. Here is the if else statement.

 if (transform.position.z < 0)
     {
         rnd = Random.Range(0, 5);
         if(rnd == 0)
         {
             if(madeHole == false)
             {
                 renderer.enabled = false;
                 collider.isTrigger = true;
                 madeHole = true;
             }
             else
             {
                 renderer.enabled = true;
                 collider.isTrigger = false;
                 madeHole = false;
             }
         }
         else
         {
             renderer.enabled = true;
             collider.isTrigger = false;
             madeHole = false;
         }
         
         transform.position.z += 150;
     }

Any help or insight could be useful. Thank you.

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

1 Reply

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

Answer by Fattie · Mar 06, 2013 at 10:55 AM

First of all,

you can be utterly certain that there is NO problem with the if statement in Javascript, Mono or Unity3D.

You've made some mistake.

Add debugging statements:

 Debug.Log("I am here, point A");

and for example ..

 Debug.Log("Now the value of blah is ... " + x );

In this way you can learn more and solve the problem.

When you are learning to program you need to constantly include Debug.Log statements. Essentially every other line of code should be a Debug line.

You'll have the problem solver within an hour, cheers

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 JonJacob · Mar 06, 2013 at 06:40 PM 0
Share

Thank you for the insight. After some well needed rest I came back to it and realized that since I had attached the script to each individual block to get it to move they all had their separate madeHole bool so that when the next block came through it would use it's own bool which was of course false. I fixed it by making madeHole a global variable in another script. Thank you again.

avatar image Fattie · Mar 06, 2013 at 07:44 PM 0
Share

cheers hope it helps bud

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

11 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

Related Questions

I'm wondering with if function working 2 Answers

MouseLook half disabling 1 Answer

Gap in requirements in GUI based game 2 Answers

Why is an action in my "if" block executing in the "else" conditions? 2 Answers

Power-Ups scripting error 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