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 boddole · Jan 22, 2014 at 08:38 AM · c#if-statements

IF Statement Being Ignored

Hello everyone, I've come across a problem where one of my IF statement conditions is being ignored, and the code within it is being run even though the condition is false.

The purpose of these scripts is to: disable the player, turn the screen black, move the player to the spawn, and then allow the player to move.

The first code block contains the IF statement that is being ignored [if (screenFadeInOut.proceed == true)]. And the second block is the script that is primarily referenced by the first. Any insight is appreciated.

     void IsPlayerDead ()
     {
         if (isDead || currentHealth<=0f)
         {
             playerMovement.enabled = false;
             Debug.Log ("Player cannot move");
 
             screenFadeInOut.sceneEnding = true;
             Debug.Log ("Screen getting dark");
 
             if (screenFadeInOut.proceed == true)
                 Debug.Log ("Ending scene now");
             {
                 rigidbody.velocity = Vector3.zero;
                 Debug.Log ("The player has no speed");
                 
                 transform.position = lastKnownSpawn;
                 Debug.Log ("Moving player");
                 
                 isDead = false;
                 Debug.Log ("Player is now undead");
                 
                 //screenFadeInOut.sceneStarting = true;
                 //Debug.Log ("Starting scene now");
                 playerMovement.enabled = true;
             }
     
             //disable controls, fade screen, reset position, reset room objects, turn screen back on, unlock controls
         }
     }


     public void EndScene () 
     {
         if (sceneEnding == true)
         {
         guiTexture.enabled = true;
         FadeToBlack();
             if (guiTexture.color.a >= 0.95f)
             {
                 guiTexture.color = Color.black;
                 //guiTexture.enabled = false;
                 sceneEnding = false;
                 proceed = true;
             }
         }
     }
     
Comment
Add comment · Show 1
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 Jamora · Jan 22, 2014 at 10:12 AM 0
Share

So are you saying that regardless of screenFadeInOut.proceed being true or false, "Ending scene now" is being printed to the console?

3 Replies

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

Answer by CHPedersen · Jan 22, 2014 at 10:26 AM

Oh, I just realized something else which is probably the real cause. Notice that you must never put lines of code between an if-sentence and its associated block of code. The reason is that an if-sentence followed by a single line of code (not delimited by curly braces) will be interpreted by the compiler as that if-statement's block. I.e.:

 if(someCondition == true)
 Debug.Log("I am the if-statement's block");

Is equivalent to:

 if(someCondition == true)
 {
     Debug.Log("I am the if-statement's block");
 }

And this:

 if(someCondition == true)
 Debug.Log("I am the if-statement's block");
 {
 // Code here is not at all associated with the if-statement
 }

Is equivalent to:

 if(someCondition == true)
 {
 Debug.Log("I am the if-statement's block");
 }
 // Code here is not at all associated with the if-statement

To avoid this type of confusion in the future, always delimit all code for an if-sentence by curly braces {}, and do not put anything between the if-statement and its braces.

When used to just encapsulate otherwise free-standing code, curly braces instead *define a scope*, that is, a section of code with its own memory stack, which cleans up the memory allocated by variables declared inside of it when control flow leaves the scope. Declaring such a scope is what you have unknowingly done in your code above.

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 boddole · Jan 23, 2014 at 02:37 AM 0
Share

You are completely correct. The syntax I was using completely invalidated the scope of the script.

avatar image
1

Answer by jaroosh · Jan 22, 2014 at 11:08 AM

Hey,

Ive just noticed, not sure if this is a typo here or its exactly like this in the code BUT :

 if (screenFadeInOut.proceed == true)
 Debug.Log ("Ending scene now");
 {

what it does is : if the proceed is true, it will print Debug info and then regardless of the condition it will start a scope ({) and proceed withe the rest of the code. What you should to here is :

  if (screenFadeInOut.proceed == true)
  {
     Debug.Log ("Ending scene now");
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 boddole · Jan 23, 2014 at 02:38 AM 0
Share

You and CHPedersen are correct, it was the syntax I was using.

avatar image
0

Answer by Ebil · Jan 22, 2014 at 08:53 AM

I dont think this statement is being ignored, but you assume different results. Why dont you debug the values before and after the statement starts? Why do you check if player is dead or has < 0 HP? isdead should be the only value which is checked, anything else is no good way to handle this state.

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 boddole · Jan 22, 2014 at 09:04 AM 0
Share

I can see that [if (screenFadeInOut.proceed == true)] is false because I can watch it in the inspector (both the bool value and the alpha value of the guiTexture, and the code inside that block runs well before the condition even comes close to being achieved.

Also, I get what you are saying about isDead vs 0 HP, I wrote it that way because the player can take damage, but also be killed instantly. And rather than give those "instant kills" arbitrary high values, I just decided to make them trigger the bool directly.

avatar image CHPedersen · Jan 22, 2014 at 10:20 AM 0
Share

.Net's runtime does not ignore if-sentences, and it does not evaluate conditions wrong. The bug is either in your code or in the way Unity serializes the value of screenFadeInOut.proceed to the Inspector.

Put screenFadeInOut.proceed in a call to Debug.Log just prior to the if-sentence. I'm positive it is indeed true when the if-sentence executes, and false when it doesn't.

avatar image boddole · Jan 23, 2014 at 02:39 AM 0
Share

I knew I was the problem, I just didn't see where I was causing it. Thank you to you and jaroosh for pointing it out to me.

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

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

Related Questions

A node in a childnode? 1 Answer

C# Character Controller Collision If Statement 2 Answers

C# if If Statement is True Declare Variable 1 Answer

Distribute terrain in zones 3 Answers

C# Declaring Variables Within If Statements 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