0

After the execution of code following exception occurred at the end of code, I have do a lot of changes in the code but nothing change at all.

NullReferenceException: Object reference not set to an instance of an object RocketController.Start () (at Assets/Script/RocketController.cs:10)

What is the cause of this exception?

using UnityEngine;
using System.Collections;

public class RocketController : MonoBehaviour {

// Use this for initialization
Rigidbody2D rd;
void Start () {

    rd.GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {

    if(Input.GetKey("right"))
    {
        rd.velocity = new Vector2(1,0);
    }
    else if(Input.GetKey("left"))
    {
        rd.velocity = new Vector2(-1,0);
    }
    else
    {
        rd.velocity = new Vector2(0,0);
    }


  }//close update
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Is `rd` ever instantiated? It seems to be always null in your code, so doing `rd.anything` will throw that exception. – Andrew Sep 05 '16 at 16:32
  • 1
    The irony of the comment `// Use this for initialization` right above the problem. :) – Drew Kennedy Sep 05 '16 at 16:34
  • you have mistaken how GetComponent works you need to call that from the controller and assign it to the rd field, not call it from the null rd field – MikeT Sep 05 '16 at 16:37

2 Answers2

2

UPDATE: Here is working code:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody2D))]
public class RocketController : MonoBehaviour {

// Use this for initialization
Rigidbody2D rd;
void Start () {

    rd = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update () {

    if(rd == null)
        return;
    if(Input.GetKey("right"))
    {
        rd.velocity = new Vector2(1,0);
    }
    else if(Input.GetKey("left"))
    {
        rd.velocity = new Vector2(-1,0);
    }
    else
    {
        rd.velocity = new Vector2(0,0);
    }


  }//close update
}
Umair M
  • 10,298
  • 6
  • 42
  • 74
  • Include above line before class definition i got these error: Attribute 'RequireComponent' is not valid on this declaration type. It is only valid on 'class' declarations. – user3662189 Sep 05 '16 at 16:44
  • @user3662189 I guess you were not writing it at correct place, I have updated my answer. check it – Umair M Sep 05 '16 at 16:48
  • This code is not working i simply just copy and paste but its not working at all - here is the repeat error : NullReferenceException: Object reference not set to an instance of an object – user3662189 Sep 05 '16 at 16:51
  • on which line is it now? – Umair M Sep 05 '16 at 16:55
  • in the end of code after else – user3662189 Sep 05 '16 at 16:58
  • just add a null check in update like i did. – Umair M Sep 05 '16 at 17:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122689/discussion-between-umair-m-and-user3662189). – Umair M Sep 05 '16 at 17:04
  • i got my answer simply add the component of rigid body in unity and its all work fine with my old code thanks buddy for your time and valuable answer – user3662189 Sep 05 '16 at 17:16
  • `[RequireComponent(typeof(Rigidbody2D))]` actually adds the component automatically if you dont attach it manually. This should have worked :) – Umair M Sep 05 '16 at 17:18
  • 1
    @UmairM if the annotation was added after the script was attached to a object I don't think it will automatically add the component. – Scott Chamberlain Sep 05 '16 at 17:26
1

The variable rd is never instantiated so it is null. You need to instantiate it before you can use it.

A NullReferenceException means that, the thing you are trying to work with is null.

The way to instantiate a rigid body in Unity is to it as follow: rd = GetComponent<Rigidbody2D>(); More info

Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
  • this is exactly how initialization work in Unity. either you assign component from inspector. or you use `GetComponent`. OP is just using `GetComponent` in wrong manner. – Umair M Sep 05 '16 at 16:37
  • i have change it but it remains same as before MissingComponentException: There is no 'Rigidbody2D' attached to the "rocket" game object, but a script is trying to access it. You probably need to add a Rigidbody2D to the game object "rocket". – user3662189 Sep 05 '16 at 16:41
  • @user3662189 that means the object that the script is attached to has no `Rigidbody2D`, so you need to add one to the object or not use `Rigidbody2D` – Scott Chamberlain Sep 05 '16 at 16:43
  • @user3662189 follow [my answer](http://stackoverflow.com/a/39334574/4366237). – Umair M Sep 05 '16 at 16:45