Im Getting an error saying: Object reference not set to an instance of an object although nothing is null.

So… i am following this tutorial on youtube and i am getting stuck at making the camera. I have tried to add the camera script before the player script in the Script execution order but i still get the same error on line 20. When i press play the script is disabled in the inspector and when i enable it i get another error in line 42 which is added repeatedly.

Here is the camera script:

public class ThirdPersonCamera : MonoBehaviour {


	[SerializeField] Vector3 cameraOffset;
	[SerializeField] float damping;

	Transform cameraLookTarget;


	Player localPlayer;


	void Awake () 
	{
		GameManager.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;;
		cameraLookTarget = localPlayer.transform.Find ("cameraLookTarget");

		if (cameraLookTarget == null)
			cameraLookTarget = localPlayer.transform;



	}



	void HandleOnLocalPlayerJoined(Player player)
	{
		localPlayer = player;
	}



	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);
	}

}

Here is the Player script:

[RequireComponent(typeof(CharacterMovement))]
public class Player : MonoBehaviour {


	[System.Serializable]
	public class MouseInput{
		public Vector2 Damping;
		public Vector2 Sensitivity;
	}


	[SerializeField] float speed;
	[SerializeField] MouseInput MouseControl;


	private CharacterMovement m_characterMovement;
	public CharacterMovement characterMovement
	{
		get{
			if (m_characterMovement == null) 
			{
				m_characterMovement = GetComponent<CharacterMovement> ();
			}
			return m_characterMovement;
		}
	}

	InputController playerInput;


	void Awake () 
	{
		playerInput = GameManager.Instance.inputController;
		GameManager.Instance.LocalPlayer = this;
	}
	

	void Update () 
	{
		Vector2 direction = new Vector2 (playerInput.Vertical * speed, playerInput.Horizontal * speed);
		characterMovement.Move (direction);
	}
}

I am positive that the errors has to do with the “cameralooktarget” but i would like to know why and how i could fix it. Tell me if you need anymore details, any kind of help is appreciated.

Your main problem is that you set your localPlayer variable inside the “HandleOnLocalPlayerJoined” callback. However you just subscribed the callback inside Awake and right after you try to access the localPlayer variable which of course is not set yet. You should move all the remaining code inside Awake into “HandleOnLocalPlayerJoined”.

Another big problem may be your “GameManager” singleton. How is “Instance” implemented? Does it lazy load? If it’s a MonoBehaviour and it does not lazy load you don’t want to access it inside Awake of other classes since the initialization order of the classes matters.

You can use the Script Execution Order to avoid such problems, but when the project gets bigger this will quickly get out of control.

Am I right that “OnLocalPlayerJoined” is invoked from inside the setter of “GameManager.LocalPlayer”? That would require a strict order of:

  • GameManager
  • ThirdPersonCamera
  • Player

If “Player” would initialize before “ThirdPersonCamera”, you would have already assigned the LocalPlayer in the GameManager before ThirdPersonCamera has subscribed to the event.