In my Unity game I have a vehicle prefab working with Edy's Vehicle Physics asset. I'm trying to instantiate a chosen vehicle prefab on the position and rotation of a GameObject.
When running my code in my scene to spawn a vehicle I get this error
ArgumentException: The Object you want to instantiate is null
ArgumentException: The Object you want to instantiate is null. UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:239) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:151) UnityEngine.Object.Instantiate[VehicleController] (EVP.VehicleController original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206) VehicleSpawner.spawnVehiclesInFirstGarage () (at Assets/Scripts/VehicleSpawner.cs:44) HandleGarage.Start () (at Assets/Scripts/HandleGarage.cs:59)
Vehicle Prefab is empty because the vehicle will be chosen by the user in game. (Even if I select a vehicle prefab I still get same error)
My code for instantiating the vehicle prefab on the game object
public EVP.VehicleTelemetry telemetryComponent;
public EVP.VehicleController vehiclePrefab;
public GameObject spawnObject;
public void spawnVehiclesInFirstGarage ()
{
// Load saved JSON
string jsonData = SecurePlayerPrefs.GetString ("vehicleNames");
// Convert to Class
Database loadedData = JsonUtility.FromJson<Database> (jsonData);
// Loop through Owned Vehicles Garage
for (int i = 0; i < loadedData.vehicleNames.Count; i++) {
// Set up instantiate
var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));
vehiclePrefab = vehicle as EVP.VehicleController;
// Spawn vehicle on game object
EVP.VehicleController newVehicle = Instantiate(vehiclePrefab, spawnObject.transform.position, spawnObject.transform.rotation) as EVP.VehicleController;
telemetryComponent.target = newVehicle;
}
}
var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));
, what doesDebug.Log(vehicle);
yield? – Edwin Chua