I'm making a flight simulator and trying to make a chase cam with it. When I run the c# code it gives me this error --> NullReferenceException: Object reference not set to an instance of an object Plane.Pilot.Update () (at Assets/PlanePilot.cs:14) and the 14th line of code is Camera.main.transform.position = moveCamTo; how do I get rid of the error?
Asked
Active
Viewed 439 times
0
-
2possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – O. R. Mapper Mar 01 '15 at 18:54
-
... or [this](http://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean) or [this](http://stackoverflow.com/questions/8206810/object-reference-not-set-to-an-instance-of-an-object) or [this](http://stackoverflow.com/questions/20140047/how-to-solve-object-reference-not-set-to-an-instance-of-an-object) or [this](http://stackoverflow.com/questions/1031336/what-is-the-meaning-of-nullreferenceexception) or [this](http://stackoverflow.com/questions/15735570/object-reference-not-set-to-an-instance-of-an-object) ... – O. R. Mapper Mar 01 '15 at 18:56
-
... or [this](http://stackoverflow.com/questions/18413769/object-reference-not-set-to-an-object-calling-razor-model-from-view) or [this](http://stackoverflow.com/questions/7932823/nullreferenceexception-was-unhandled-by-user-code) or [this](http://stackoverflow.com/questions/17557083/c-sharp-object-reference-not-set-to-an-instance-of-an-object-error) or [this](http://stackoverflow.com/questions/22408075/nullreferenceexception-was-unhandled-by-user-code-object-reference-not-set-to) ... – O. R. Mapper Mar 01 '15 at 18:58
-
... or [this](http://stackoverflow.com/questions/4466878/object-reference-not-set-to-an-instance-of-object-when-using-a-listt-in-c-shar) or (approximtely 50 more) - when you are asked to check whether your question has been answered before, **please do so**. – O. R. Mapper Mar 01 '15 at 18:58
1 Answers
1
This means that you are assigning null to Camera.main.transform.position (it's trying to reference an object, but it can only reference to null, causing the exception).
You need to assign a value to the moveCamTo variable before assigning it to Camera.main.transform.position.
You can also test to make sure the variable isn't null before assigning it:
if (moveCamTo != null) {
//moveCamTo is not null - you can assign it
}

Cameron
- 557
- 7
- 19