Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

How do i use more than 1 "if" in javascript coding?

Discussion in 'Scripting' started by JustChris, Sep 13, 2011.

  1. JustChris

    JustChris

    Joined:
    Sep 11, 2011
    Posts:
    5
    Title says it For example

    if (Input.GetButtonDown ("Fire1"))
    (Do Something)

    And

    if (Input.GetButtonDown ("Fire2"))
    (Do Something Else)

    So like if i do this that happens AND if i do that this happens
    How would i put it in together as I cannot "function Update ()" More than once.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    26,726
    Code (csharp):
    1.  
    2. if (thing)
    3. {
    4.   do this;
    5.   do this;
    6.   do this;
    7.   do this;
    8. }
    9.  
    10. if (thing  otherThing)
    11. {
    12.    do stuff;
    13. }
    14.  
    is and.
     
  3. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    Update is called continuously once per frame, you don't continually call it, as in you never should say,

    Code (csharp):
    1. if (x > 1)
    2.      Update();
    switch statements work like
    Code (csharp):
    1. if(x == 1) {/*...*/}
    2. if(x == 2) {/*...*/}
    in a clean way:

    Code (csharp):
    1. var someVar : int = 0;
    2. function Update() {
    3. switch(someVar) {
    4.     case 0:
    5.     print("someVar = 0");
    6.     break;
    7.     case 1:
    8.     print("someVar = 1");
    9.     break;
    10. }
    11. }
     
  4. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    Also,

    if you don't know, || is or:

    Code (csharp):
    1. if(case == 0 || case ==1) {
    2.      print("case = 0 or 1.");
    3. }
     
  5. majmun

    majmun

    Joined:
    Feb 2, 2011
    Posts:
    140
    Code (csharp):
    1.  
    2. void Update ()
    3. {
    4.     [COLOR="blue"]if [/COLOR](Input.GetKey (KeyCode.Mouse2)) Debug.Log ("mouse2");
    5.  
    6.     [COLOR="blue"]if [/COLOR](Input.GetKey (KeyCode.Mouse3)) Debug.Log ("mouse3");
    7.  
    8.     [COLOR="blue"]if [/COLOR](Input.GetKey (KeyCode.Mouse4)) Debug.Log (Input.mousePosition.y);
    9.  
    10.     [COLOR="blue"]if [/COLOR](Input.GetKey (KeyCode.Mouse5)) Debug.Log ("mouse5");
    11.  
    12.     [COLOR="blue"]if [/COLOR](Input.GetKey (KeyCode.Mouse6)) Debug.Log ("mouse6");
    13. [COLOR="seagreen"]// the "||" sign is same as "or" so if any condition is true it will read next lines[/COLOR]
    14.         [COLOR="blue"]if [/COLOR](Fileon == true [COLOR="blue"]|| [/COLOR]Editon == true [COLOR="blue"]|| [/COLOR]MapSize == true [COLOR="blue"]|| [/COLOR]Settings == true [COLOR="blue"]|| [/COLOR]Helpon == true [COLOR="blue"]|| [/COLOR]Exiton == true [COLOR="blue"]|| [/COLOR]Saveon == true [COLOR="blue"]|| [/COLOR]Loadon == true) enablebuilding = false;
    15.     [COLOR="blue"]else [/COLOR]enablebuilding = true;
    16. [COLOR="seagreen"]// the "" sign is same as "and" so if all conditions are true it will read next lines[/COLOR]
    17.     [COLOR="blue"]if [/COLOR](Input.GetMouseButtonDown(0) [COLOR="blue"] [/COLOR]Input.GetKey (KeyCode.LeftShift) == false [COLOR="blue"] [/COLOR]Input.mousePosition.y > Screen.height/5+5 [COLOR="blue"] [/COLOR]Input.mousePosition.y < Screen.height-40 [COLOR="blue"] [/COLOR]toolmainInt==0 [COLOR="blue"] [/COLOR]enablebuilding == true)
    18.     {
    19.              Debug.Log ("BLA BLA BLA");
    20.     }
    21. }
    22.  
    if this doesn't explains then sry...
     
  6. eTag96

    eTag96

    Joined:
    Oct 24, 2010
    Posts:
    110
    Code (csharp):
    1. if(explains == false) {
    2.      i_am_sorry = true;
    3. }
     
unityunity