How to assing GameObject to script within script?

I have a Public Transform target that is an empty child on my player GameObject. The transform is in the script for my camera. I want the script to check if the target variable is not assigned and if it’s not I want to assign the target object to the variable.

I tried:

public Transform target;

void Update () {

        if (target == null)
        {
            target = GameObject.FindGameObjectWithTag("PlayerTarget").transform;
        }
}

Nvm I found the answer on my own.

 void Update(){
         target = GameObject.FindGameObjectWithTag("PlayerTarget").transform;
    }

No need to check if it’s not assigned.