Search Unity

Problem with Rigidbody2D

Discussion in 'Getting Started' started by VironFlo, Sep 9, 2015.

  1. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    Hello, i have a problem with RigidBody2D

    ______________________________
    #pragma strict
    var ToucheSol:System.Boolean=false;
    var AnimePerso:UnityEditor.GameObjectUtility;
    function Start () {

    }

    function Update () {

    if(Input.GetKeyDown(KeyCode.Space)&& ToucheSol == true){

    GetComponent<Rigidbody2D>AddForce(Vector2(0,2000)); // Operator '<' cannot be used with a left hand side of type 'function(System.Type): UnityEngine.Component' and a right hand side of type 'System.Type'.
    }
    if(Input.GetKey(KeyCode.RightArrow)){

    transform.Translate(Vector2(0.04,0));
    }
    if(Input.GetKey(KeyCode.LeftArrow)){

    transform.Translate(Vector2(-0.04,0));
    }

    }
    function OnCollisionEnter20(Coll:Collision2D){
    if(Coll.gameObject.tag == "Sol"){
    ToucheSol=true;
    }
    }
    function OnCollisionExit2D(Coll:Collision2D){
    if (Coll.gameObject.tag == "Sol"){
    ToucheSol=false;
    }
    }
    ___________________________________

    Can you help me please? i'm a beginner :)
     
  2. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    I have anothers error : unknow identifier 'addforce'
    sorry i can't edit my post.
     
  3. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    Up plox help me :D
     
  4. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    The line should be: GetComponent<Rigidbody2D>().AddForce(Vector2(0,2000));

    Note the added ().; GetComponent is a method so you need parentheses, and then the dot accesses methods of the object returned by GetComponent.
     
    JoeStrout likes this.
  5. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    Thanks very much, this problem is resolved, but one message still say : Error UCE0001: ';' expected. Insert a semicolon at the end. (UCE0001) (Assembly-UnityScript)
    at GetComponent().<Rigidbody2D>AddForce(Vector2(0,2000));

    Juste between C and E of AddForce
     
  6. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    No you put them in the wrong place; the angle brackets <> are part of the GetComponent method, not part of the AddForce method. Look at my post and write that exactly.
     
  7. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    Okay , i write like you the lines but hmm...
    3 news errors :s

    GetComponent<Rigidbody2D>().AddForce(Vector2(0,2000));

    Error BCE0043: Unexpected token: ).

    Error BCE0044: expecting ), found '.'.

    Error UCE0001: ';' expected. Insert a semicolon at the end.

    I'm sorry, i know i'm bad ^^
     
  8. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    I don't think just writing Vector2(0,2000) is valid; that's a constructor, so you may need a 'new' keyword in there.

    More generally, where did you get this code from anyway? You're getting confused by lots of tiny errors, so you probably should look for a new resource with better code.
     
    Last edited: Sep 9, 2015
  9. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    I follow a video, where everything work exept this command, because the software Unity get udapted( version 4.20 ) , but write vector2(0,2000) work on the video for me
     
  10. VironFlo

    VironFlo

    Joined:
    Sep 9, 2015
    Posts:
    10
    I'm really worried, everytime, the lines with rigidbody is telling a error,
    is it possible to get a script with the jump please?

    thanks
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Do you need to specify the type of forcemode you are using? I use c# but I'm sure I've done it by specifying the vector & the force mode.

    From the unity scripting api
    public function AddForce(force: Vector2, mode: ForceMode2D = ForceMode2D.Force): void;
    public void AddForce(Vector2 force, ForceMode2D mode = ForceMode2D.Force);