1
votes

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)

script

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;
    }
}
1
After var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));, what does Debug.Log(vehicle); yield?Edwin Chua
I get the name of the selected prefab "Sport Coupe (UnityEngine.GameObject)"WokerHead
Could you show the full class?Edwin Chua
that's the only code being used to instantiate. I call the function when the scene is startedWokerHead
I added the full error message in the question. Maybe it'll help.WokerHead

1 Answers

1
votes

Possible Solutions

  1. The vehiclePrefab = vehicle as EVP.VehicleController; could be causing problems, as logging var vehicle = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i]));, already gives Sport Coupe (UnityEngine.GameObject), which is the correct type.

    Instead of using as EVP.VehicleController, try:

    // Set up instantiate
    var vehiclePrefab = Resources.Load("Prefabs/" + (loadedData.vehicleNames [i])) as GameObject;
    
    // Spawn vehicle on game object
    GameObject newVehicle = Instantiate(vehiclePrefab, spawnObject.transform.position, spawnObject.transform.rotation);
    EVP.VehicleController controller = newVehicle.GetComponent<EVP.VehicleController>();
    telemetryComponent.target = newVehicle;
    

    This is from Unity's documentation for loading prefabs via scripting.

  2. From the error message, it seems that spawnVehiclesInFirstGarage() is being called in void Start(). If so, try putting it in the void Update(), adding a if( prefab != null && !isInstantiated) check.

Additional Troubleshooting (if the solutions don't work)

  • Try assigning a prefab via the inspector and see if you get the same problem.
  • On your existing code, try Debug.Log(vehiclePrefab).