- Home /
The infamous NullReferenceException again
We all know and love the NullReferenceException don't we?
here's the code that produces it :
var stoppuhr : System.Diagnostics.Stopwatch;//declaring like a baws.
here comes the NullReference
stoppuhr.Start(); // NullReferenceException
Any help is appreciated,
thanks,
bio
Answer by CHPedersen · Jul 26, 2011 at 08:17 AM
Like I suggested in your other question about keeping track of time, this occurs because you're trying to access a membermethod of an object you haven't instantiated yet. You've only declared the variable, and then told the system that the variable is of type Stopwatch. You haven't actually MADE the object yet.
You have to instantiate it with the "new" keyword before you can use it, see this list of UnityScript keywords:
http://www.unifycommunity.com/wiki/index.php?title=UnityScript_Keywords#new
That is, I suspect you need something like the following, keeping in mind that I don't usually code in JS:
var stoppuhr : System.Diagnostics.Stopwatch = new System.Diagnostics.Stopwatch();
If it worked out for you, could you please click to accept the answer as correct? ;) It motivates people a lot more to help you if you remember to use the upvotes and accept people's answers every once in a while.
sorry, forgot that! SHA$$anonymous$$E ON $$anonymous$$E!
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
null reference exception ... help! 2 Answers
NullReferenceException help 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers