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

Player controller error

Discussion in 'Scripting' started by Gozalez, May 29, 2017.

  1. Gozalez

    Gozalez

    Joined:
    May 29, 2017
    Posts:
    5
    Mentioned code:

    Code (CSharp):
    1. public class Movement : MonoBehaviour
    2. {
    3.     private Rigidbody2D rb2d;
    4.     private Collider2D col;
    5.     public float speed;
    6.     public float jump;
    7.     public int LayerMask;
    8.  
    9.     private void Start()
    10.     {
    11.         rb2d = GetComponent<Rigidbody2D>();
    12.     }
    13.     void FixedUpdate()
    14.     {
    15.  
    16.         float moveHorizontal = Input.GetAxis("Horizontal");
    17.         float moveVertical = Input.GetAxis("Vertical");
    18.         Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    19.         rb2d.AddForce(movement * speed);
    20.         if (col.IsTouchingLayers(LayerMask = Physics2D.AllLayers) && Input.GetKeyDown(KeyCode.Space))
    21.         {
    22.             rb2d.AddForce(Vector2.up * Time.deltaTime * jump);
    23.         }
    24.  
    25.     }
    26.  
    27.  
    28. }
    It's my first player controler so please help
     
  2. Gozalez

    Gozalez

    Joined:
    May 29, 2017
    Posts:
    5
    Console reports:
    NullReferenceException: Object reference not set to an instance of an object
    Movement.FixedUpdate () (at Assets/Movement.cs:24)
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You forgot to assign the collider2d :)
     
  4. Gozalez

    Gozalez

    Joined:
    May 29, 2017
    Posts:
    5
    How can I do it?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think what you should first do is get the exact type (which type of Collider2d is it?) Then set the variable type to that.
    If it is a box collider, you'd do:
    Code (csharp):
    1.  public BoxCollider2D col;
    Then, just drag the gameobject into that slot in the inspector. Let me know if that works.
     
  6. Gozalez

    Gozalez

    Joined:
    May 29, 2017
    Posts:
    5
    My script looks like that now

    Code (CSharp):
    1.  
    2. public class Movement : MonoBehaviour
    3. {
    4.     private Rigidbody2D rb2d;
    5.     public BoxCollider2D col;
    6.     public float speed;
    7.     public float jump;
    8.     public int LayerMask;
    9.  
    10.     private void Start()
    11.     {
    12.         rb2d = GetComponent<Rigidbody2D>();
    13.         col = GetComponent<BoxCollider2D>();
    14.     }
    15.     void FixedUpdate()
    16.     {
    17.  
    18.         float moveHorizontal = Input.GetAxis("Horizontal");
    19.         float moveVertical = Input.GetAxis("Vertical");
    20.         Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    21.         rb2d.AddForce(movement * speed);
    22.         if (col.IsTouchingLayers(LayerMask = Physics2D.AllLayers) && Input.GetKeyDown(KeyCode.Space))
    23.         {
    24.             rb2d.AddForce(Vector2.up * Time.deltaTime * jump);
    25.         }
    26.  
    27.     }
    28.  
    29.  
    30. }
    31.  
    There is no error on console but player dosen't move
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Does "speed" variable have a value > 0?
    Is the mass of your object rather large?
    Is "Is Kinematic" unchecked (shouldn't be checked) on your rigidbody?
    Are any constraints on the rigidbody checked (ex. freeze x / y / z)?
    This script of course is on your player (i.e. dragged on the player)?
    Friction?
     
    Last edited: Jun 5, 2017
  8. Gozalez

    Gozalez

    Joined:
    May 29, 2017
    Posts:
    5
    Yes, yes and yes. Player starting on the air and gravity should move him but it doesn't work
     
    Last edited: Jun 6, 2017