Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

if statement in a if statment?

Discussion in 'Scripting' started by RankXenon, Jan 27, 2013.

  1. RankXenon

    RankXenon

    Joined:
    Jan 20, 2013
    Posts:
    48
    is it possible to make a if statement in a f statement?

    something like this:

    Code (csharp):
    1. function Update () {
    2.     if(Input.GetKeyDown("space")){
    3.         if(gameObject.GetComponent("emptyScript").enabled = true){
    4.             print("emptyScript is enabled");
    5.         }
    6.         else(gameObject.GetComponent("emptyScript").enabled = false){
    7.             print("emptyScript is disabled");
    8.         }
    9.     }
    10. }
     
  2. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    Sure it is possible and I use it really often. I don't understand your question, because you posted the code above. It's your code? Why do you don't test yourself? When I'm trying out something and it don't work for days I post my question here normally.

    You could always test out such things with Debug.Log(); and get a message what's going on.
     
  3. RankXenon

    RankXenon

    Joined:
    Jan 20, 2013
    Posts:
    48
    I thoght that it isn't possible cause i get 3 errors that doesen't make sense :c

    1. On (3.67) BCE0044: expecting ), found "=".
    2. On (3.69) BCE0043: unexpected token: true.
    3. On (4.56) BCE0043: expecting :, found ";".
     
  4. RankXenon

    RankXenon

    Joined:
    Jan 20, 2013
    Posts:
    48
    I thoght that it isn't possible cause i get 3 errors that doesen't make sense :c

    1. On (3.67) BCE0044: expecting ), found "=".
    2. On (3.69) BCE0043: unexpected token: true.
    3. On (4.56) BCE0043: expecting :, found ";".
     
  5. Elryk

    Elryk

    Joined:
    Jan 2, 2013
    Posts:
    82
    check your operators = is for setting values, == is for checking values
     
  6. Ricks

    Ricks

    Joined:
    Jun 17, 2010
    Posts:
    650
    Another error is that you added a condition after "else". This is not allowed. The quick solution is to write "else if" instead of "else", but I seriously suggest to learn the basics of programming first before diving into game development...
     
    Last edited: Jan 27, 2013
  7. Swearsoft

    Swearsoft

    Joined:
    Mar 19, 2009
    Posts:
    1,632
    These two should fix it for you.
     
  8. MikeBastien

    MikeBastien

    Joined:
    Apr 24, 2009
    Posts:
    139
    To summarize Ricks and Elryk's posts:

    Code (csharp):
    1.    
    2. function Update () {
    3.         if(Input.GetKeyDown("space")){
    4.             if(gameObject.GetComponent("emptyScript").enabled == true){
    5.                 print("emptyScript is enabled");
    6.             }
    7.             else{
    8.                 print("emptyScript is disabled");
    9.             }
    10.         }
    11.     }
     
  9. Alex Cruba

    Alex Cruba

    Joined:
    Aug 16, 2011
    Posts:
    564
    I think you should train the structure with the fixed script from Mike.