I need help to call my camera target

Hello,

I’m trying to change the target of my main camera to set it on the instanciated player. So i did this :

private Camera2DFollow cam;

then in a private void awake :

cam = Camera.main.GetComponent();

And after, in the private void Instantiate player() :

cam.target = playerInstantiate;

But unity says the reference is not set to an instance of an object…

Where am I wrong ?

Are you sure the Camera2DFollow component is on your main Camera object?