using UnityEngine; using UnityEngine.Rendering.PostProcessing; public class CarSelectionManager : MonoBehaviour { [Header("Car Selection")] public CarData[] allCarData; // Array of ScriptableObject car stats public GameObject[] allCarPrefabs; // Array of car prefabs public Transform spawnPoint; // Where to spawn the car [Header("Camera")] public CameraController cameraController; // Cached reference to the PostProcessVolume private PostProcessVolume cachedVolume; private void Start() { // Cache the PostProcessVolume from the main camera cachedVolume = Camera.main.GetComponent(); int selectedIndex = GameConstants.CarSelected; // Safety check if (selectedIndex < 0 || selectedIndex >= allCarPrefabs.Length || selectedIndex >= allCarData.Length) { Debug.LogError("Selected car index is out of bounds!"); return; } // Instantiate the selected car GameObject carInstance = Instantiate( allCarPrefabs[selectedIndex], spawnPoint.position, spawnPoint.rotation, spawnPoint ); // Reset local transform carInstance.transform.localPosition = Vector3.zero; carInstance.transform.localRotation = Quaternion.identity; // Initialize VehicleController with CarData VehicleController controller = carInstance.GetComponent(); if (controller != null) { controller.InitializeWithData(allCarData[selectedIndex]); // Setup camera cameraController.target = controller.transform; cameraController.InitializeTarget(); } else { Debug.LogError("VehicleController not found on the instantiated car prefab."); } // Assign PostProcessVolume to NOSController NOSController nos = carInstance.GetComponent(); if (nos != null) { if (cachedVolume != null) { nos.SetPostProcessVolume(cachedVolume); Debug.Log("Assigned PostProcessVolume to NOSController."); } else { Debug.LogWarning("PostProcessVolume not found on the main camera."); } } else { Debug.LogWarning("NOSController not found on the instantiated car."); } } }