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

Error=NullReferenceExeption: Object reference not set to an instance of an object. Please Help!

Discussion in 'Scripting' started by SmexyBob, Apr 21, 2016.

  1. SmexyBob

    SmexyBob

    Joined:
    Apr 21, 2016
    Posts:
    5
    using UnityEngine;
    using System.Collections;

    public class PlayerController : MonoBehaviour {

    private Rigidbody rb;

    void start ()
    {
    rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate ()
    {
    float moveHoziontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHoziontal, 0.0f, moveVertical);

    rb.AddForce (movement);
    }
    }
    Im making my first game and its giving me this weird error NullReferenceExeption: Object reference not set to an instance of an object and im not sure what to do. Please help me I don't understand.
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    Null reference exceptions mean that you have a reference variable that has nothing assigned to it. You didn't say where the error is occurring, but I would assume it's the rigidbody causing it.

    Since you're assigning to it using GetComponent, there must be a Rigidbody component attached to the same GameObject the PlayerController is on. If not, it won't be found and the rb reference will be null.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    c# is case sensitive

    Start

    not

    start
     
  5. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    And make sure the object does in fact have a rigidbody in it.

    Also, if I'm not mistaken, you need to normalize your direction otherwise you'll be going faster when moving in angles.

    Try this instead:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class PlayerController : MonoBehaviour {
    7.  
    8. private Rigidbody rb;
    9. public float speed;
    10.  
    11. void start (){
    12. rb = GetComponent<Rigidbody>();
    13. }
    14.  
    15. void FixedUpdate (){
    16.  
    17. float moveHoziontal = Input.GetAxis("Horizontal");
    18. float moveVertical = Input.GetAxis("Vertical");
    19. Vector3 movement = new Vector3(moveHoziontal, 0.0f, moveVertical);
    20.  
    21. // Set the movement vector based on the axis input.
    22. movement.Set (moveHorizontal, 0f, moveVertical);
    23.  
    24. // Normalise the movement vector and make it proportional to the speed per second.
    25. movement = movement.normalized * speed * Time.deltaTime;
    26.  
    27. // Move the player to it's current position plus the movement.
    28. rb.addForce (movement);
    29.  
    http://unity3d.com/learn/tutorials/projects/survival-shooter/player-character?playlist=17144
     
    Last edited: Apr 21, 2016
  6. SmexyBob

    SmexyBob

    Joined:
    Apr 21, 2016
    Posts:
    5
    how would i connect the two together
     
  7. SmexyBob

    SmexyBob

    Joined:
    Apr 21, 2016
    Posts:
    5
    and the error is in the line rb.addforce (movement);