- Home /
[Solved]NullReferenceException problem
Hello.
I have a script attached to planets that makes camera go from "Solar view" to "Planet view" in a smooth lerp. Here are pictures of both views:
Here's the script:
using UnityEngine;
using System.Collections;
public class CamSol : MonoBehaviour {
public Vector3 DefPos = new Vector3(0,600,0);
public Camera Cam;
public Transform Target;
public float MinDis = 50f;
private float MoveChange = 0f;
private float t;
public float RandomFloat;
void Start () {
}
void OnMouseUp(){
MoveChange++;
}
void Update () {
Camera Cam = Camera.current;
transform.RotateAround(new Vector3(0,0,0), Vector3.up, RandomFloat * Time.deltaTime);
Transform Target = transform.transform;
if(MoveChange == 1f ){
t+=Time.deltaTime;
Vector3 Smth = Target.transform.position - Cam.transform.position;
Quaternion Rot = Quaternion.LookRotation(Smth);
Cam.transform.rotation = Quaternion.Lerp(Cam.transform.rotation, Rot, t/5);
Vector3 Pos = new Vector3(Target.transform.position.x, Target.transform.position.y, Target.transform.position.z -MinDis);
Cam.transform.position = Vector3.Lerp(DefPos, Pos, t * 1.5f);
}
if(MoveChange == 2f){
t = 0f;
t+=Time.deltaTime;
Quaternion Rot = Quaternion.Euler(90,0,0);
Cam.transform.position = Vector3.Lerp(Cam.transform.position, DefPos, t*1.5f);
Cam.transform.rotation = Quaternion.Lerp (Cam.transform.rotation, Rot, t*1.5f);
}
if(MoveChange == 3f){
MoveChange = 1f;
}
}
}
[Edit] Problem solved, you need to change Camera.current into Camera.main
The probelm is, that if I try to go to Planet view in editor I get NullReferenceException Error and the camera is spazzing in the game.
Thanks in advance for any answears.
-Xentarok
P.S: Sorry for any mistakes.
P.S.2: If you have any questions or I didn't explain something in enough detail, write it in the comments.
On a side note, at first glance your "exit" button looks like it says "shit".
Full error message: NullReferenceException UnityEngine.Component.get_transform () (at C:/BuildAgent/work/7535de4ca26c26ac/Runtime/ExportGenerated/Editor/UnityEngineComponent.cs:21) CamSol.Update () (at Assets/Game/$$anonymous$$ody/CamSol.cs:69)
In your script, what does line 69 correspond with in the code you posted?
Well, since the error is with get_transform, it suggests that you are trying to use the transform of some object that doesn't exist. This leads me to the conclusion that in line 66 above,
Cam.transform.position = Vector3.Lerp(Cam.transform.position, DefPos, t*1.5f);
is the error, since the compiler thinks Cam doesn't exist.
Based on the documentation, I'm not entirely sure what Camera.current should actually return. What do you expect it to give you?
Answer by tw1st3d · Jul 31, 2013 at 06:12 PM
void Update () {
Camera Cam = Camera.current;
should be
void Update () {
Cam = Camera.current;
I believe, cause it looks like you're definine "Cam" twice.
Answer by aibolith · Apr 16, 2017 at 04:57 PM
I had a similar problem: I was taking Camera.current.transform.rotation to adjust characters UI labels, and it was working, but it was giving me the null refference exception every single frame. I changed it to Camera.main and the exception vanished.
What I wonder is how is possible for the code to work even though it says that I'm trying to refference a null object? Where did it take correct .transform.rotation , if the object was null?