- Home /
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;
}
}
Answer by denewbie · Nov 06, 2010 at 04:23 PM
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 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();
- the "ship" If this was missing then you dont have a game object named "Ship" in your scene.
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!
Answer by IJM · Nov 05, 2010 at 10:36 PM
Do you have a GameObject in the scene named "Ship"?
GameObject ship = GameObject.Find("Ship");
That could be it.
I do, it never leaves the scene either, there is always a ship as long as anything is on screen.
Your answer
Follow this Question
Related Questions
Scriptable object saves but what about objects it references? 0 Answers
Do you have to set a object reference for every script? 0 Answers
Is a unity object really destroyed if its destructor was never called? 1 Answer
The infamous: Object Reference not set to an instance of an object 1 Answer
Object Rotation/Character Speed 1 Answer