NullReferenceException while trying to access a Component

I’ve been at this for hours, now, and I don’t feel like I’m getting closer to understanding the underlying problem here. Running this script gives me: “NullReferenceException: Object reference not set to an instance of an object” on lines 31 and 15.

What am I doing wrong here?

Update: I’ve tried moving the alpha stuff out of that function but, unfortunately, the error persists. Updated the code example to what I’ve currently got. Also updated the error above.

Update 2: Related to this issue, I believe, is another script I’ve created wherein I both can print the instance of an object into the console and, simultaneously, Unity returns a NullReferenceException. In the example below, thisLight is apparently null but correctly prints into the console. My head.

Update 3: Have tried everything I could think of, can’t figure out why it’s throwing this error. Does anyone have the knowledge to pinpoint where this is going wrong?

Code from Update2

thisLight = GameObject.Find("BrightCone");

Original Problem’s Code

var hasTriggered: boolean = false;
var hasFaded: boolean = false;
var msg: String = "";

var tempAlpha: float = 0.0f;
function Start(){
	guiText.font.material.color.a = 0;
	guiText.text = msg;

function Update(){
			tempAlpha = guiText.text.material.color.a + Time.deltaTime;

function OnTriggerEnter(c:Collider){
   if(c.gameObject.tag == "Player"){
		hasTriggered = true;

function FadeIn(){
	if (hasFaded == false){
		guiText.text.material.color.a = tempAlpha;
	else {hasFaded = true;}

Delete gameObject. from your script.

text property of the GUIText might be null

Re: error on line 31:


.text is a string, it has no material

you want either guiText.material (more likely) or guiText.font.material

If you are using C# you may need to use a temporary variable instead of the += operator.


float tempAlpha;
tempAlpha = gameObject.guiText.text.material.color.a + Time.deltaTime;
gameObject.guiText.text.material.color.a = tempAlpha;