- Home /
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");
print(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(){
if(hasTriggered){
if(!hasFaded){
FadeIn();
}
else{
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;}
}
Answer by xandermacleod · May 20, 2013 at 04:01 PM
If you are using C# you may need to use a temporary variable instead of the += operator.
i.e.
float tempAlpha;
tempAlpha = gameObject.guiText.text.material.color.a + Time.deltaTime;
gameObject.guiText.text.material.color.a = tempAlpha;
In C# you can not use color.a and set it, you should set color completely due to being a value type
But is that relevant to JS? If not, though it's a valuable tip, I'm not sure how it applies directly to my problem above since I'm not using C# in my script?
Oh, unless I misread you and you're replying to xander?
Answer by Graham-Dunnett · May 20, 2013 at 10:42 AM
Delete gameObject.
from your script.
I thought I tried that before but guiText demanded that I give it an instance of a game object to attach to. I removed it per your instructions anyway, however:
Regardless of whether gameObject exists or not I still get NullReferenceExceptions on line 28. Specifically: NullReferenceException: Object reference not set to an instance of an object
Answer by Ashkan_gc · May 20, 2013 at 06:34 PM
text property of the GUIText might be null
Doesn't appear to be.
GUIText [It is enabled, in case it matters]
Text: WD-404
Anchor: middle center
Alignment: center
etc ...
Display Fading Text <-- this script
Script: displayFadingText
Has Triggered: N
Has Faded: N
$$anonymous$$sg: WD-404
Answer by Loius · May 30, 2013 at 03:47 PM
Re: error on line 31:
GUIText: http://docs.unity3d.com/Documentation/ScriptReference/GUIText.html
.text is a string, it has no material
you want either guiText.material (more likely) or guiText.font.material
Aha! That may be the source of that particular problem, aye. I'll check that out later today.