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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object reference not set to an instance of an object

Discussion in 'Scripting' started by tallieke, Jan 23, 2016.

  1. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    Hey, so I have a problem. Iv'e just started making a game, and already can't get my controller/motor scripts to work. Heres is the error code I'm getting

    "NullReferenceException: Object reference not set to an instance of an object
    PlayerController.Update () (at Assets/PlayerController.cs:29)"

    Yeah, so here's the scripts, (playercontroller first)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(PlayerMotor))]
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     [SerializeField]
    7.     private float speed = 5;
    8.  
    9.     private PlayerMotor motor;
    10.  
    11.     void Start ()
    12.     {
    13.         motor = GetComponent<PlayerMotor> ();
    14.     }
    15.  
    16.     void Update ()
    17.     {
    18.         //Calculate movement velocity as a 3d Vector
    19.         float _xMov = Input.GetAxisRaw("Horizontal");
    20.         float _zMov = Input.GetAxisRaw("Vertical");
    21.  
    22.         Vector3 _movHorizontal = transform.right * _xMov;
    23.         Vector3 _movVertical = transform.forward * _zMov;
    24.  
    25.         //final movement vector
    26.         Vector3 _velocity = (_movHorizontal + _movVertical.normalized * speed);
    27.  
    28.         //apply movement
    29.         motor.Move(_velocity);
    30.        
    31.     }
    32.  
    33.  
    34. }
    35.  


    And here is the motor script

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. [RequireComponent(typeof(Rigidbody))]
    5. public class PlayerMotor : MonoBehaviour {
    6.  
    7.     private Vector3 velocity = Vector3.zero;
    8.  
    9.     private Rigidbody rb;
    10.  
    11.     void Start ()
    12.     {
    13.         rb = GetComponent<Rigidbody> ();
    14.     }
    15.  
    16.     //Gets a movement vector
    17.     public void Move (Vector3 _velocity)
    18.     {
    19.         velocity = _velocity;
    20.     }
    21.  
    22.     //Run every physics iteration
    23.     void FixedUpdate ()
    24.     {
    25.         performMovement ();
    26.     }
    27.     //Perform movement based on velocity variable
    28.     void performMovement ()
    29.     {
    30.         if (velocity != Vector3.zero)
    31.         {
    32.             rb.MovePosition (transform.position + velocity * Time.fixedDeltaTime);
    33.         }
    34.     }
    35.    
    36.  
    37. }
    38.  

    Any help is greatly appreciated :)
     
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Have you tried just using

    Code (CSharp):
    1. motor = GetComponent(PlayerMotor);
    in your Start() function? Sometimes there are problems when trying to use GetComponent the way you have it.
     
  3. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    So that made the error go away but made 3 new ones.

    here they are

    Assets/PlayerController.cs(13,38): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    Assets/PlayerController.cs(13,25): error CS1502: The best overloaded method match for `UnityEngine.Component.GetComponent(System.Type)' has some invalid arguments

    Assets/PlayerController.cs(13,25): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Type'


    Do you know how i could solve these.. But either way thanks for the help
     
  4. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Try
    Code (CSharp):
    1. GetComponent("PlayerMotor") as PlayerMotor;
    ...?

    I think I accidentally gave you a JavaScript format before. Oops.
     
  5. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37

    Thank you so much, that fixed my problem, but there is a little bit more. Currently I can only walk forwards and backwards, not sideways, do you know why I'm not able to walk sideways?
     
  6. tallieke

    tallieke

    Joined:
    Aug 17, 2014
    Posts:
    37
    Scratch that, it works great now, i just had to change the input for horizontal, whoops. But thanks so much for the help ::)