NullReferenceException: Object reference not set to an instance of an object CameraController.LateUpdate () (at Assets/Scripts/CameraController.cs:17)
Hi everyone,
I am new to Unity and I am trying the "Roll A Ball" Tutorial on the Unity website. I am not able to get the camera to follow the ball.
My CameraController.cs file contains:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
//This line has the error.
transform.position = player.transform.position + offset;
}
}
Also both in my project and the tutorial, the GameObject for the ball is called "Player", whereas the code above defines GameObject player. Do we need to link up player with Player? Sorry if these questions are really basic but I am stuck at this for a while now.
Any help is really appreciated. Thanks for your time. :)
Regards, Faisal
Answer by eldruz · Mar 01, 2017 at 02:40 PM
The public variable player is declared, but not instantiated inside the script. This means that you must give it a reference from the editor, here you want to reference the player the camera is supposed to follow.
This is explained in the tutorial itself at the 4:35 minute mark : link to video
Have fun!
Your answer
Follow this Question
Related Questions
Coroutine Issue 1 Answer
3rd person camera for Roll a Ball 1 Answer
How to make the ball follow the cursor? 0 Answers