6 Replies - 517 Views - Last Post: 26 March 2013 - 09:06 PM Rate Topic: -----

#1 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Unity & using C# _Braces question

Posted 26 March 2013 - 08:13 PM

Hello,

I was wondering why this code would not recognize the else statement without taking the braces out?
I realize single lines don't really need them but why would it not work with them? I am using unity and c#
            void Grounded() // PlayerState Run would not trigger without removing braces
        {
            if (playerState == PlayerState.Run)
            {
                if (!animation.IsPlaying("jogging"))
                { //this one
                    State = PlayerState.Run;
                    animation.Play("run2"); 
                } //this one
            }
            else // WOULD NOT trigger the PlayerState Idle with the braces either
            { 
                 if (!animation.IsPlaying("land"))
                {//this brace
                    State = PlayerState.Idle;
                    animation.Play("idle"); 
                } //this brace
            } 
        }



well it's not a huge problem I can remove these and it works fine but I was reviewing for a day or so why my animations were not working correctly and why my state was not triggering from a falling to landed state properly.

Just more wondering why cause this makes me want to Not put the extra braces in if not needed in anything I do?

Is This A Good Question/Topic? 0
  • +

Replies To: Unity & using C# _Braces question

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,811
  • Joined: 12-June 08

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 08:17 PM

Lines 5-9 do not have an else statement. they are inside the scope of the if of line 3. The else of line 11 matches up with line 3.
Was This Post Helpful? 0
  • +
  • -

#3 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 08:49 PM

View Postmodi123_1, on 26 March 2013 - 08:17 PM, said:

Lines 5-9 do not have an else statement. they are inside the scope of the if of line 3. The else of line 11 matches up with line 3.



yea my bad I found the else not firing issue first then after the animations were working properly and were not in a constant falling state Noticed the landing not triggering with those braces.

But when I took them out now everything is triggering fine so was my braces a mistake to be there then?
if not then I wonder why they were not firing with them in place?

thanks
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,811
  • Joined: 12-June 08

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 08:52 PM

Would you write that in a coherent manner? I have read that three times and still do not know what you are asking. I am not sure what 'they' and 'them' reference.
Was This Post Helpful? 0
  • +
  • -

#5 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 08:56 PM

they and them are referring to the {} I marked in the code

just really after if those braces were a mistake?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9287
  • View blog
  • Posts: 34,811
  • Joined: 12-June 08

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 08:58 PM

If you don't have either set of braces the second lines in they contain will happen regardless... right? The compiler will see an 'if' and no braces so it will execute, conditionally, the very next line.. and only that line. Lines 8 and 16 would happen regardless of the if condition (assuming you took out both sets of braces).
Was This Post Helpful? 1
  • +
  • -

#7 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: Unity & using C# _Braces question

Posted 26 March 2013 - 09:06 PM

Well I realize now that I will wait until Morning to finish this, it was not firing with the proper braces in place because of a separate error, no wonder the braces in question were missed when you read my question.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1