,How to make a camera follow a instantiated object

,Hello everyone!
This is my first time making something in unity, it has been quite fun. But I have ran into some trouble I havent been able to fix, when I spawn in my player my camera wont follow it, the camera script works just fine if the player is already in the scene before the game starts.

    public Transform target;
    public float cameraSpeed = 2f;
    public Vector3 offset = new Vector3(0, 2, -7);
    public GameObject player = GameObject.Find("PlayerCube(Clone)");
    private GameObject playerHolder;
    bool initialSpawn = false;

    void Start () {
        Debug.Log("This works");
	void Update () {
        target = player.transform;
        Vector3 desiredPosition = target.position + offset;
        Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, (cameraSpeed * Time.smoothDeltaTime) );
        transform.position = smoothedPosition;

if anyone could help me out or give me some pointers I would be much appreciated

You must be destroying your player object, so target == null because player is no longer after you destroy it. Within the script where you instantiate your new player you can have a reference to the camera follow script and set the target to the new player:

public CameraFollow camFollowSrcipt;
GameObject newPlayer;

void Update(){
newPlayer = Instantiate(prefabName, position);
camFollowScript.target = newPlayer.transform;

I have a follow up question, apologies if this is in the wrong place or if I have not provided enough detail, I am still very new to all this!

I am trying to do the “Using C# to launch projectiles” tutorial. Doing it by the letter is easy and works.

I am trying to go a bit above and beyond and get the camera to follow the cannonball that gets instantiated, and if two cannonballs are fired, to follow the ‘average position’ of the two (or three). Now, that is a secondary issue. But how can I get the camera to follow the cannonball?

Link to tutorial: Using C# to launch projectiles - Unity Learn

public class LaunchProjectile : MonoBehaviour
   public GameObject projectile;
   public float launchVelocity = 700f;
   void Update()
       if (Input.GetButtonDown("Fire1"))
           GameObject ball = Instantiate(projectile, transform.position,  
           ball.GetComponent<Rigidbody>().AddRelativeForce(new Vector3 
                                                (0, launchVelocity,0));

Hi @xephosmmb122 . The problem is that you cannot do GameObject.Find("PlayerCube(Clone)"); before the script has started, which means that you should do that statement in the Awake() or Start() methods. Awake() is the best but you’ll commonly see it in Start().

Although C# and Visual Studio won’t give you an error for this, you should have seen a red error message in Unity telling you this. Do always have a look at the bottom of the screen or in the console for errors (in red) and warnings (in orange/yellow).

void Awake()
     player = GameObject.Find("PlayerCube(Clone)");