x


Stupid question... Object reference not set to an instance of an object

I'm really not sure why I'm getting this error, it's a very short script and I can't find a single thing wrong with it. I get a new instance of this error every single frame. The code works fine though, which is even more baffling.

void Update () {
    if (this.GetComponent<BaseStatFunctions>().isPicked == false)
    {
        this.transform.position = new Vector3(transform.position.x, transform.position.y, 0);
        this.transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
    }
    else if (this.GetComponent<BaseStatFunctions>().isPicked == true)
    {
        GameObject ship = GameObject.Find("Ship");
        this.transform.rotation = ship.transform.rotation;
    }
}
more ▼

asked Nov 05 '10 at 08:57 PM

Kyouri gravatar image

Kyouri
31 5 5 9

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

try this and look at what the console prints out

void Update () {

    BaseStatFunctions myScript = GetComponent<BaseStatFunctions>();

    if (myScript ) // Checks if your script is null
   {
        if ( !myScript.isPicked)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, 0);
            transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
         }
         else
         {
             GameObject ship = GameObject.Find("Ship");
             if (ship)   // Checks if your ship exits
                   transform.rotation = ship.transform.rotation;
             else
                  print("ship does not exits");

         }
    }
    else
        print("Script does not exits");
}

The point its to check if the objects you're using http://exist.in this part the 2 objects would be: 1. the "GetComponent()" If this was missing then you might have forgotten to attach this script to the SAME GameObject. if you're calling it from another game object you should use this: GameObject.Find( "name of the gameObject" ).GetComponent();

  1. the "ship" If this was missing then you dont have a game object named "Ship" in your scene.
more ▼

answered Nov 06 '10 at 04:23 PM

denewbie gravatar image

denewbie
717 3 3 17

This solved it, with a little extra debugging I found the object causing the error, this script was accidentally attached to my camera as well as every other object. It didn't have a stats script. Thanks!

Nov 07 '10 at 11:54 PM Kyouri
(comments are locked)
10|3000 characters needed characters left

Do you have a GameObject in the scene named "Ship"?

GameObject ship = GameObject.Find("Ship");

That could be it.

more ▼

answered Nov 05 '10 at 10:36 PM

IJM gravatar image

IJM
1.4k 2 5 19

I do, it never leaves the scene either, there is always a ship as long as anything is on screen.

Nov 07 '10 at 10:57 PM Kyouri
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1089
x261

asked: Nov 05 '10 at 08:57 PM

Seen: 1481 times

Last Updated: Nov 05 '10 at 08:57 PM