Using list of transforms for transform.lookat.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public GameObject[] cars;
    Transform target;
    int selectedCar;
    
    void Start(){
        selectedCar = PlayerPrefs.GetInt("selectedCar");
        target = cars[selectedCar].transform;
    }
    
    void Update(){
        transform.LookAt(target);
    }
}

In my game I have a character select scene where I run through a list of objects and when one is selected it is stored into an int which I labeled selectedCar which is an index in the list cars. In the script above I am basically accessing that selectedCar variable and getting its transform so that I can use the transform.LookAt function and make the camera follow the player. In the inspector I can see that when the scene loads up it does in fact set “target” to the transform of the selected car but the camera does not follow. I tested my code without a list just purely dragging and dropping a transform in and that worked fine. Im curious as to why it will not work even though the camera has a target(transform) to follow.

Update I found a solution.

public class GameManager : MonoBehaviour
{
    
    public GameObject[] carPrefs;
    public GameObject targetObj;
    public Transform spawn;

    void Start()
    {
        int selectedCar = PlayerPrefs.GetInt("selectedCar");
        GameObject prefab = carPrefs[selectedCar];
        GameObject clone = Instantiate(prefab, spawn.position, Quaternion.identity);
        targetObj.transform.SetParent(clone.transform, true);
    }
}

in the game manager script where I instantiate my selected car I just set an empty game object to be my target transform and set it as a child to the selected cars transform. Maybe not the best fix but it works for sure. I at least got to do away with the super weird and complicated camera script this required way less code and works now.