- Home /
NullReferenceException After A Few Hours?
Alright, so I have this very annoying problem that keeps occurring after an hour or so. I have 3 3D Text Objects that are being changed via the Update() method, and everything works great until after a certain amount of time that causes this line right here:
GetComponent<TextMesh>().text = "Fuel: " + playerScript.Fuel.ToString("f1") + "%";
And 2 others that are closely like it to return a NullReferenceException. I can't find a solution once it occurs, UNLESS I restart Unity, which is making absolutely no sense whatsoever. Anybody care to shine some light on the subject?
you could add some Debug.Log()
's into it to find out which part is causing it... the GetComponent()
or playerScript
could be null at some point.
also, any reason you're doing GetComponent()
and not using some cached version of it?
EDIT: annoyingly chrome removed the < Text$$anonymous$$esh >
that i add (without spaces)
gif: for anything that I use only once every few seconds (like updating based on pickups, which this might be) I prefer to embed the GetComponent. Overall runs about as fast; easier to read and verify; avoids errors where you remake the item and forgot to update the saved link.
OP: hmmm...if restarting Unity fixes it, but Stop then Play again doesn't, maybe you're changing a prefab somewhere (which I think breaks it in the editor until you restart.)
@Owen Reynolds, I suppose it could have been that, as I was having some prefab conflictions, this occurred around 3-4 times before it seemed to have ceased, but I didn't keep any time on it or anything.. Since then we threw out a majority of the system and started doing it a different way. $$anonymous$$aybe it was a Unity bug, maybe it was me touching something without knowing and the issue co$$anonymous$$g up later. Hasn't happened since.
Answer by Lachee1 · Jul 13, 2014 at 10:04 AM
can't help too much without the rest of the code and error. For example I don't know what is updating playerScript.Fuel, or if playerScript is changing at all.
It does seem though that your playerScript variable changes to null, as its the only thing that can be null! So make sure that it isn't being set to something that is null.
I don't think you're understanding my issue, nothing is always null, after an allotted amount of time I get these errors without touching anything related to it (to my knowledge at least). I understand the primary OOP concepts and what not, this is an issue with either me, or Unity breaking something in the editor that causes this until a restart takes place.