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

NullReferenceExpectation: Object reference not set to an instance of an object

Discussion in 'Scripting' started by sick12345, Jan 4, 2013.

  1. sick12345

    sick12345

    Joined:
    Jan 4, 2013
    Posts:
    14
    Hello, I am not good with scripts but I know how to use them. I am trying to make a 3rd person shooter so I put this script on my character and I placed a camera and I get this error when I try to play it.

    "NullReferenceExpectation: Object reference not set to an instance of an object soldier script.update () ( at Assets/ soldier script.js : 13"


    I used this script

    HTML:
    }var speed = 3.0;
    var rotateSpeed = 3.0;
    function Update () {
    var controller : CharacterController = GetComponent(CharacterController);
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = speed * Input.GetAxis ("Vertical"); 
    controller.SimpleMove(forward * curSpeed);
    }
    @script RequireComponent(CharacterController)
    
    Thanks in advance
     
  2. madmike6537

    madmike6537

    Joined:
    Aug 20, 2012
    Posts:
    49
    What line is the error on? It says line 13 but what line is that?

    My only guess would be that its because you are setting the variable controller every frame ( because its in the update function). Try putting that line into the start function

    function Start()
    {
    var controller : CharacterController = GetComponent(CharacterController);
    }
     
    Last edited: Jan 4, 2013
  3. sick12345

    sick12345

    Joined:
    Jan 4, 2013
    Posts:
    14
    Here is the lines numbered.

    1. #pragma strict
    2.
    3. function Start () {
    4.
    5. var speed = 3.0;
    6. var rotateSpeed = 3.0;
    7. function Update () {
    8. var controller : CharacterController = GetComponent(CharacterController);
    9. transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    10. var forward = transform.TransformDirection(Vector3.for ward);
    11. var curSpeed = speed * Input.GetAxis ("Vertical");
    12. controller.SimpleMove(forward * curSpeed);
    13. }
    14. @script RequireComponent(CharacterControlle
     
  4. carljohnfred

    carljohnfred

    Joined:
    Dec 28, 2012
    Posts:
    31
    It's easier to read the code if you use code tags like this:

    Code (csharp):
    1. #pragma strict
    2.  
    3. function Start () {
    4.  
    5. var speed = 3.0;
    6. var rotateSpeed = 3.0;
    7. function Update () {
    8. var controller : CharacterController = GetComponent(CharacterController);
    9. transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    10. var forward = transform.TransformDirection(Vector3.for ward);
    11. var curSpeed = speed * Input.GetAxis ("Vertical");
    12. controller.SimpleMove(forward * curSpeed);
    13. }
    14. @script RequireComponent(CharacterControlle
    As you can see, this is much easier to read and find problems. I'll assume the 'Vector3.for ward' was a typing mistake when you posted the code, otherwise you probably would've mentioned an error about it. It looks like your real problem is that there is no closing squiqqly bracket (sorry, I don't know the technical term) at the end of the Start() function. Also, I'm not sure why you're declaring those variables in the start function. If you declare them outside of any function, you can set/change the values in the Inspector window (even while running the game).
     
    Last edited: Jan 4, 2013