Question by
redx050 · Jun 22, 2020 at 04:55 AM ·
nullreferenceexceptionthird-personthird-person-camerathird-person-shooterthird person shooter
Camera for a Third Person Shooter dosen't work
Hi everyone, I am new to Unity and I am trying the "Third-person shooter" Tutorial on youtube.when I start the game I can't control anything as the game doesn't work and sent me a message contains"NullReferenceException: Object reference not set to an instance of an object thirdpersoncamera.Update ()". My CameraController.cs file contains:
[SerializeField] Vector3 cameraOffSet;
[SerializeField] float damping;
Transform cameralooktarget;
Player LocalPlayer;
void Awake()
{
Gamemanager.Instance.OnLocalPlayerJoined += HandleonLocalPlayerJoined;
}
void HandleonLocalPlayerJoined(Player player)
{
LocalPlayer = player;
cameralooktarget = LocalPlayer.transform.Find("cameralooktarget");
if (cameralooktarget == null)
cameralooktarget = LocalPlayer.transform;
}
// Update is called once per frame
void Update()
{
Vector3 targetPosition =
cameralooktarget.position +
LocalPlayer.transform.forward * cameraOffSet.z +
LocalPlayer.transform.up * cameraOffSet.y +
LocalPlayer.transform.right * cameraOffSet.x;
transform.position = Vector3.Lerp(transform.position, targetPosition, damping * Time.deltaTime);
}
Comment
Answer by Zoedingl · Jun 22, 2020 at 11:26 AM
In unity in the inspector, you must link up the public fields.