Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 26, 2016 at 01:17 PM by Funkeeh for the following reason:

Too subjective and argumentative

avatar image
0
Question by Funkeeh · Jul 13, 2016 at 12:10 PM · if-statementsif-else

Why is my if-statement still true when i set it to false, and wont move to the next else if-statement?

So when i start this, the code stays in the first if statement, even though i recognises the change of the boolean values. What am i doing wrong?

public class TrainingManager : MonoBehaviour {

 public static float        waitTimer                    = 2.0f;
 private bool                s_01_beginSequence    = false;
 private bool            s_02_visualiseBtn            = false;

 private float waitBeforeStim         = 1.00f;            //Wait timers (amount of time)
 private float waitBeforeRestart      = 2.00f;
 private float waitBeforeStimActual   = 0.0f;            //Wait timers (initial start time)
 private float stimulate              = 0.0f;
 private float showButtonClr          = 0.0f;
 private float restart                = 0.0f;

 void Update () 
 {
     if ( s_01_beginSequence == true && s_02_visualiseBtn == false) 
     {
         // wait until it is 1.0
         if (waitBeforeStimActual < waitBeforeStim) 
         {
             waitBeforeStimActual += Time.deltaTime;
         }

         if (waitBeforeStimActual > waitBeforeStim) 
         {
             if (stimulate >= waitTimer) 
             {
                 stimSound.Stop ();

                 s_02_visualiseBtn     = true;
                 s_01_beginSequence     = false;
             }

             else if (stimulate < waitTimer) 
             {
                 stimulate += Time.deltaTime;
                 if (!stimSound.isPlaying) 
                 {
                     stimSound.Play ();
                 }
             } 
         }
     }
     else if ( s_01_beginSequence == false && s_02_visualiseBtn == true )
     {
         if (waitBeforeStimActual > waitBeforeStim) 
         {
             if (showButtonClr >= waitTimer) 
             {
                 stimSound.Stop ();

                 s_02_visualiseBtn     = false;
                 s_01_beginSequence     = true;

                 stimulate         = 0.0f;
                 showButtonClr = 0.0f;
             }

             else if (stimulate < waitTimer) 
             {
                 showButtonClr += Time.deltaTime;
                 if (!stimSound.isPlaying) 
                 {
                     stimSound.Play ();
                 }
             } 
         }
     }
     if ( Input.getKeyDown ("space") ) 
     {
         s_01_beginSequence     = true;
     }



Comment
Add comment · Show 5
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 Funkeeh · Jul 13, 2016 at 11:00 AM 0
Share

it should be note that the s_01_beginSequence bool becomes true from another script

avatar image rmassanet Funkeeh · Jul 13, 2016 at 01:06 PM 0
Share

It is hard to say without seeing all the code. But I can assure you that if the script enters the first if is because s_01_beginSequence is true and s_02_visualiseBtn is false.

So, what you're seeing is probably that you're not changing the values you think you are.

Can you post the code where the values are changed?

avatar image Funkeeh rmassanet · Jul 14, 2016 at 06:35 AM 0
Share

The value changes within the if-statement itself; in the nested if statement: if (stimulate <= waitTimer) {..} Line 25. And these changes are made when the "stimulate" variable is increased to a specific value. (2 seconds i think).. The else if statement on line 43 does the same as its if-statement above, just the opposite

Show more comments

3 Replies

  • Sort: 
avatar image
1

Answer by DenisTribouillois · Jul 13, 2016 at 01:16 PM

 private static bool        s_01_beginSequence    = false;

If it becomes true from another script, you should change the to public:

 public static bool        s_01_beginSequence    = false;
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 DenisTribouillois · Jul 13, 2016 at 12:58 PM 0
Share

You can make sure your variable is correctly set by printing it just before the if statement:

 print(s_01_beginSequence);
 if ( s_01_beginSequence == true && s_02_visualiseBtn == false) 
avatar image Funkeeh DenisTribouillois · Jul 14, 2016 at 06:37 AM 0
Share

the "private static" is me mis-typing when i had to rewrite it in the question. It is public :). Im using the Debug.Log, and the values are visually changing when the timer (stimulate += Time.deltaTime; ) lets the code enter the if-statement in line 25. But the code does not continue to the next "else if"

avatar image
1

Answer by rmassanet · Jul 15, 2016 at 07:54 AM

I've tested your code and it does not stay in the first if statement. When the time limits are reached it proceeds to the else statement.

However, there is a very rare case in which your code could get stuck in the first if statement. That remote possibility is when waitBeforeStimActual == waitBeforeStim. If that was the problem, it can easily be fixed by changing one of the two inner if statements, to cover the equality case. Although remote, it is worth covering the case and avoid spurious application fails.

I hope this helps.

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 Funkeeh · Jul 15, 2016 at 09:23 AM 0
Share

In your test, did it even go back and forth between both statements in a loop? If i change the else-if to a normal if statement, I am able to make it go through both statements, but only once - and then nothing happens

avatar image rmassanet Funkeeh · Jul 15, 2016 at 10:34 AM 0
Share

I didn't check that because you said the problem was that it did not leave the first if statement.

The answer is no it never goes back, because the code never tells it to go back:

 else if ( s_01_beginSequence == false && s_02_visualiseBtn == true )
      {
          if (waitBeforeStimActual > waitBeforeStim) {
          }
 }

Note that when the code enters the else if statement waitBeforeStimActual is already larger than waitBeforeStim, and so, obediently, it does nothing.

avatar image
0

Answer by Funkeeh · Jul 15, 2016 at 09:26 AM

Just for an update: I re-wrote the code and used a switch-case instead of if the if - else-if statements, and that seemed to work. So Switch-case 1 - 0 if-statements :)

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 rmassanet · Jul 15, 2016 at 10:23 AM 0
Share

That doesn't make any sense. It's not if-else's fault. When you rewrote the code you probably fixed an error. Can you post your working code?

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

If else block acts extremely weird 0 Answers

Need a object to react different when clicked mutliple times 1 Answer

HELP ME PLEASE TO FIX MY PROBLEM.. 1 Answer

How to number players(Simple Probably) 0 Answers

How to use IF in Unity for selection MyScript 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