- Home /
Null Reference Exception - Time.deltatime problem
I'm new to unity and I'm in need of help with javascript. Everytime i use in my code Time.deltatime i get a following error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs) DestroyAfterTimeScript.Update () (at Assets/Scripts/DestroyAfterTimeScript.js:12)
From my understanding of this error i tried to access a reference variable that isn't referencing any object. Yet I stumble upon this error every time i use Time.deltatime in my code, which should work just fine, since i'm referring to a time function. Or maybe I'm wrong, and the problem is much more simple, but since I get this error only when using Time.deltatime, i can't figure out what I'm doing wrong.
The script isn't named "Time", or anything like this. (I red about similar problem, where the solution was to just change the name of the script from "Time" to something else, but it's not the case since my script is named "DestroyAfterTimeScript")
This code is supposed to destroy the object after a certain number of seconds, with a little bit of randomization. So, here it is:
var destroyAfterTime : float = 10;
var destroyAfterTimeRandom : float = 0;
@HideInInspector
var countToTime : float;
function Awake () {
destroyAfterTime += Random.value * destroyAfterTimeRandom;
}
function Update () {
countToTime += Time.deltatime;
if (countToTime >= destroyAfterTime)
{
Destroy(gameObject);
}
}
Error points me to the line 12:
countToTime += Time.deltatime;
Any help would be very appreciated.
Oh, and by the way I'm following ETeeski tutorials, there you can probably find more details of the code i'm trying to get to work. He doesn't seem to have any problems, and his bulletholes disappear just fine. I copied every line of his code, and still it's not working.
Link to the tutorials: https://www.youtube.com/watch?v=ei635F6xeP8&list=PL7AE076AFAFD3C305∈dex=14
Answer by Jessespike · Aug 04, 2015 at 08:44 PM
Note that the "T" is capitalized in deltaTime
Thank you so much man! I was trying to figure it out for days, and I haven't thought even for a second the answer could be that simple. You're tha real $$anonymous$$VP! Sorry for my over enthusiasm, but it was my first big headache over an incorrect code and you just randomly appeared and solved it. Good luck in your life and whatever you are doing!