NullRefferenceException, Specific Help Needed.

Hello,
I am VERY new to code and have hardly written more than an HTML web page.

I am getting the following error in my CONSOLE:
[10142-screen+shot+2013-04-16+at+6.07.28+pm.png|10142]

I understand what ever i need is making my forward arrow not work (driving a motorcycle in the game) and that it needs to UPDATE every frame to make sure your still holding down that forward key. Problem is i have NO idea how to reference or fix this. SO new to coding you will have to explain to me exactly what to do! Thanks!

Here are the pictures of the code around the LINE that it is referencing as having a problem.
[10143-code+pic.jpg|10143]

Thanks for the Help!
I Just need to know what line I’m missing that it cant find and how to write it, I can tell you what you need to know like name of game objects, scripts, and or components.

Please reply using the coding field option :P it will save a bit of time!

Please post code in future instead of an image. Then people can test it without having to write the whole thing out .... http://answers.unity3d.com/page/newuser.html

3 Answers

3

For the error on line 104, there are 2 possibility:

  1. GetComponent.< SmoothFollow >() returns a null
  2. body is a null, so body.transform throws a null-ref exception In line 117, you use body.GetComponent() and it has no problem, so the body is not null Not exactly sure about this one. Need a Debug.Log( body != null ) to find out.

For the error on line 286:

  1. audioSource is null

+1 to the possibility of body possibly being null also.

Just noticed that the body is not null, because there is no null-ref exception from line 117: audioSource = body.GetComponent.< AudioSource >()

I was looking at that too, but wouldn't it not even get that far? I'm thinking body could be null...but I guess we'll see. :)

Okay I will test this :P and send back results :D thank you! Btw I have ALL scripts applied where they need to be and all values assigned and controls assigned as well :D

Does this shed Light on What is the null? [10147-screen+shot+2013-04-16+at+10.24.33+pm.png|10147]

In order to prevent null references you should always protect code that works with possibly unassigned references with a conditional; ‘if the thing I’m looking for is present, then do something with it’. In code, that looks like this:

if (thingImLookingFor != null)
{
    // do something with thingImLookingFor
}

In your particular case, you probably want to try the following:

SmoothFollow smoothFollow = Camera.mainCamera.GetComponent.< SmoothFollow >();
if (smoothFollow != null)
{
    smoothFollow.target = body.transform;
}

Also, since your error is occurring on Start, make sure that whatever sets the reference to body runs before you try to use it. If it's not public, and not set in the inspector, consider moving whatever sets it to Awake(). I suspect that it's whats null, however, because as long as your SmoothFollow script is attached to the mainCamera then it should find that n/p.

Hey Thanks! Unfortunately I feel like im reading a foreign language haha! I have natha experience in coding so with that said let me save a copy and try what you guys have suggested :D Thanks for the quick response!

Ah I just understood this! Let me try :D

These are all true BUT I did the simple thing as to LINK the incorrect script :P. Yes im that new to unity :P. WHat it was is there was too smooth follow scripts. One was in the folder that another script was looking for and what was in a folder that was obsolete I chose the one that was obsolete and so it threw a null :P. All I did was link proper script that was in the folder that the other script was looking for and it instantly worked. :stuck_out_tongue:

Thank you so much for your time and I’m sorry that it was something so simple and stupid! But I learned something from you guys so this is good!