Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Problem with IF statement

Discussion in 'Scripting' started by Peti89, Feb 7, 2015.

  1. Peti89

    Peti89

    Joined:
    Jun 16, 2014
    Posts:
    3
    Hi there
    I have a problem. The first if statement executes without end whether i press space or not.
    Code (JavaScript):
    1. #pragma strict
    2. public var fal : GameObject;
    3. public var spidy : GameObject;
    4. public var notToHit : LayerMask;
    5. private var anc : Vector2;
    6. public var joint : DistanceJoint2D;
    7.  
    8.  
    9. function Start ()
    10. {
    11.  
    12. }
    13.  
    14.  
    15. function Update ()
    16. {
    17.     anc.x = 0.4;
    18.     anc.y = 0;
    19.     if (Input.GetMouseButtonDown(0));
    20.     {
    21.         var hit: RaycastHit2D = Physics2D.Raycast(spidy.transform.position,transform.TransformDirection(-Vector2.right),10,notToHit);
    22.         if (hit.collider != null)
    23.         {
    24.             //rigidbody2D.AddForce (Vector2.right * 100);
    25.             joint = gameObject.AddComponent (DistanceJoint2D);
    26.             joint = GetComponent(DistanceJoint2D);
    27.             joint.distance = hit.distance;
    28.             joint.connectedAnchor = hit.point;
    29.             joint.anchor = anc;
    30.             joint.maxDistanceOnly = false;
    31.             joint.connectedBody = hit.rigidbody;
    32.             Debug.Log("Hit");
    33.            
    34.            
    35.            
    36.         }
    37.         else
    38.         {
    39.             Debug.Log("Didn't hit");
    40.         }
    41.        
    42.        
    43.        
    44.     }  
    45. }
    46.    
    Thanks for the help.
     
  2. Cinema

    Cinema

    Joined:
    Sep 19, 2013
    Posts:
    18
    I believe you shouldn't have the semi-colon after "if(Input.GetMouseButtonDown(0));" I'm not a JS guy, but if it's similiar to c# i think that may be your issue. Also, the function checks for the left mouse click, not the space.
     
  3. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    You are correct it works the same way.
     
  4. Peti89

    Peti89

    Joined:
    Jun 16, 2014
    Posts:
    3
    Oh my god, how didn't i notice that?
    Thank you for your help
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,356
    I o that all the time lol its so hard to debug it.