- Home /
Object reference not set to an instance of an object: Can't seem to work things out
I can't seem to figure out what I am doing wrong with this script. I can launch my game but whenever i am colliding with a pickup Unity closes the game and tells me there is a NullReferenceExeption going on. Been looking around for some time but can't seem to find an answer.
Here is the script ( Javascript ):
// This script is supposed to find the GUI texture in my GUIpoint GameObject?
// ( I think GUItextures are gameobjects? )
static public var onPoint : boolean = false;
static var DetectPoint : GameObject;
DetectPoint = GameObject.Find("GUIpoint");
static function hideTexture(){
if(onPoint == true){
DetectPoint.guiTexture.color.a = 255;
yield WaitForSeconds (1);
DetectPoint.guiTexture.color.a = 0;
onPoint = false;
}
}
EDIT:
The error is in the script that calls the coroutine:
GetComponent(PointGet).StartCoroutine("hideTexure");
Many thanks in advance :)
When Unity detects an error, it usually says which line the error occurs at. Can you indicate in your post which line the NullReferenceExpcetion is being reported?
Answer by kolban · May 27, 2012 at 08:56 PM
If the error is being reported in the line which reads:
GetComponent(PointGet).StartCoroutine("hideTexture");
This tells me that either:
There is no such data type (Script) called "PointGet")
The object in which GetComponent() is being called doesn't have a component of type PointGet attached to it and hence returns "null". Since it returns null, the code is equivalent to:
null.startCoroutine("hideTexture");
Which would generate the runtime error you describe.
The script where the code is on is called "PointGet" and is attached to my gameobject which is a GUI texture. That is mostly why I simply can't find a reason for this null that is happening.
Answer by rdanielsson · May 27, 2012 at 11:23 PM
Solved this error by replacing this: ( EDIT: It didn't solve anything )
GetComponent(PointGet).StartCoroutine("hideTexure");
With this:
GetComponent(PointGet);
PointGet.hideTexture();
2nd Edit:
I have replaced GetComponent(PointGet);
With:
var PointGetScript: PointGet = GetComponent(PointGet);
No errors, but it doesn't start my hideTexture function as intended, Oh and it does display the guitexture when i delete the yield WaitForSeconds (1);
in my PointGet script. I think I am triggering the coroutine wrong.
Edit #3:
The script where i am triggering the coroutine from looks like this:
if(shipTrigCollision.gameObject.tag == "TrianglePickup"){
PointGet.onPoint = true; // Sets the statement as true in our pointget script
var PointGetScript: PointGet = GetComponent(PointGet);
PointGetScript.hideTexture();
audio.PlayOneShot(trianglePickupCollideSound);
Destroy(shipTrigCollision.gameObject); // Destroys the game object we collided with last
}
And the script where the coroutine is on looks like this:
static public var onPoint : boolean = false;
static var DetectPoint : GameObject;
DetectPoint = GameObject.Find("GUIpoint");
DetectPoint.guiTexture.enabled = false; // Disables guiTexture
static public function hideTexture(){
if(onPoint == true){
DetectPoint.guiTexture.enabled = true;
yield WaitForSeconds (1);
DetectPoint.guiTexture.enabled = false;
onPoint = false;
}
}
Still very confused where the error is.
That line GetComponent(PointGet) is doing nothing at all. You already have a variable called PointGet, but GetComponent is called for a class not a variable - it's confusing because I bet your script is also called PointGet right?
Yeah, I noticed it didn't do very much :p Yes, my script is called PointGet, Isn't it that script I am refering to?
I think we need to see a bit more code to give it some context :) Can you edit the answer above or the question and post the code for both of the routines. You know how it is, it'll be something you missed in a bit you think is dead right, or something (that's what always happens to me, anyway!)
Your answer
Follow this Question
Related Questions
Access components in children of a prefab? 5 Answers
Null values in Unity 1 Answer
Resources.Load returns null in a build, but works in editor 2 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
I recieve Null Refrence Exception when I use OOP programming 2 Answers