Search Unity

Why is my code not working

Discussion in 'Scripting' started by RSharma98, May 26, 2015.

  1. RSharma98

    RSharma98

    Joined:
    Apr 27, 2014
    Posts:
    21
    Hello. I recently created a JavaScript code to make my game work on Android but it does not do what I intended it to do. I have tried everything yet for some reason my code does not work! All I want is for the code to make my player move up or down depending on which half of the screen is pressed. Does anyone know what is wrong?

    Code (JavaScript):
    1. #pragma strict
    2. var speed:float=15;
    3. function Update () {
    4. var touch : Touch;
    5. if (Input.touchCount>1) {
    6.     if (touch.position.y > Screen.width/2){
    7.           GetComponent.<Rigidbody2D>().velocity.y = speed;
    8.               }
    9.     if (touch.position.y < Screen.width/2){  
    10.               GetComponent.<Rigidbody2D>().velocity.y = speed*-1;
    11.       }
    12.     }
    13.     else{
    14.         GetComponent.<Rigidbody2D>().velocity.y=0;
    15.     }
    16. }
    I tried to export the game to my Android phone, but whenever I tap the screen the player only moves down, never up. Does anyone know what is wrong?

    I would really appreciate any help and thank you in advance if anyone knows how to fix this.
     
  2. legendz25

    legendz25

    Joined:
    May 30, 2014
    Posts:
    17
    RSharma98 likes this.
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    I'm not great with JavaScript but I don't see where you assign your touch variable to anything.

    Trying to access touch.position.y probably returns null or 0, regardless of where you touch the screen.
     
    RSharma98 likes this.