How can i make a null check for getkeys?
Posted: Wed Apr 24, 2024 7:42 pm
Hello, i would like to use getkeys, to initialize all of my vehicles in a array. The thing is, if there are no saved keys inside the folder i am trying to access i get a null reference error.
this is the code i use
and this is the error
NullReferenceException: Object reference not set to an instance of an object
ES3.GetKeys (ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:1401)
ES3.GetKeys (System.String filePath) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:1379)
Manager_Data.Vehicles_Owned_LoadAll () (at Assets/Scripts/Managers/Manager_Data.cs:154)
Thank you for your time!
this is the code i use
Code: Select all
var keys = ES3.GetKeys(SceneManager.GetActiveScene().name + "_Vehicles.es3");//the line with the error
if (keys.Length > 0)
{
for (int i = 0; i < keys.Length; i++)
{
var loadedContainer = keys[i];
var loadedVehicleContainer = ES3.Load<VehicleContainer>(loadedContainer);
GameObject newVehicleObject = Instantiate(loadedVehicleContainer.VehicleType_type.Vehicle_prefab);
newVehicleObject.GetComponent<Vehicle_ObjectScript>().AssignVehicleContainer(loadedVehicleContainer);
newVehicleObject.transform.position = GameManager.Instance.Manager_Vehicles.Vehicles_Owned_Spaces[i].transform.position;
newVehicleObject.transform.rotation = GameManager.Instance.Manager_Vehicles.Vehicles_Owned_Spaces[i].transform.rotation;
GameManager.Instance.Manager_Vehicles.Vehicles_Owned.Add(newVehicleObject);
}
}
NullReferenceException: Object reference not set to an instance of an object
ES3.GetKeys (ES3Settings settings) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:1401)
ES3.GetKeys (System.String filePath) (at Assets/Plugins/Easy Save 3/Scripts/ES3.cs:1379)
Manager_Data.Vehicles_Owned_LoadAll () (at Assets/Scripts/Managers/Manager_Data.cs:154)
Thank you for your time!