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:
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.
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;
}
}