Hello, can somebody please help ?
Here is the error :
NullReferenceException: Object reference not set to an instance of an object
public void Save()
{
Objects = 0;
objects = GameObject.FindGameObjectsWithTag("WeedPlant");
for (int i = 0; i < objects.Length; i++)
{
xPos[Objects] = objects[i].gameObject.transform.position.x;
yPos[Objects] = objects[i].gameObject.transform.position.y;
zPos[Objects] = objects[i].gameObject.transform.position.z;
fert[Objects] = objects[i].GetComponent().Fert; // ERROR
IsGrown[Objects] = objects[i].GetComponent().isGrown; // ERROR
Grams[Objects] = objects[i].GetComponent().WeedQuant;
procces[Objects] = objects[i].GetComponent().pro;
water[Objects] = objects[i].GetComponent().Water;
plantType[Objects] = objects[i].GetComponent().PlantType;
Objects++;
}
}
And the save Script :
public void Save () {
BinaryFormatter binary = new BinaryFormatter();
FileStream fStream = File.Create(Application.persistentDataPath + "/SaveFile1.dat");
SaveManager saver = new SaveManager();
saver.money = MoneyScript.money;
saver.farmBrought = BuyFarm.FarmBrought;
saver.weedOnHand = Inventory.weedOnHand;
saver.bucket = Inventory.BucketBrought;
saver.shovel = Inventory.ShovelBrought;
saver.grams = Inventory.Grams;
saver.objects = PlantWeed.Objects;
saver.x = PlantWeed.xPos;
saver.y = PlantWeed.yPos;
saver.z = PlantWeed.zPos;
saver.weedQuant = PlantWeed.Grams;
saver.isGrown = PlantWeed.IsGrown;
saver.ferti = PlantWeed.fert;
saver.pro = PlantWeed.procces;
saver.Water = PlantWeed.water;
saver.PlantType = PlantWeed.plantType;
saver.isReal = LevelManager.IsReal;
saver.objOnField = RestrictedZone.ObjOnField;
binary.Serialize(fStream, saver);
fStream.Close();
}
public void Load ()
{
if (!StartNew)
{
if (File.Exists(Application.persistentDataPath + "/SaveFile1.dat"))
{
BinaryFormatter binary = new BinaryFormatter();
FileStream fStream = File.Open(Application.persistentDataPath + "/SaveFile1.dat", FileMode.Open);
SaveManager saver = (SaveManager)binary.Deserialize(fStream);
MoneyScript.money = saver.money;
BuyFarm.FarmBrought = saver.farmBrought;
Inventory.weedOnHand = saver.weedOnHand;
Inventory.BucketBrought = saver.bucket;
Inventory.ShovelBrought = saver.shovel;
Inventory.Grams = saver.grams;
PlantWeed.Objects = saver.objects;
PlantWeed.xPos = saver.x;
PlantWeed.yPos = saver.y;
PlantWeed.zPos = saver.z;
PlantWeed.Grams = saver.weedQuant;
PlantWeed.IsGrown = saver.isGrown;
PlantWeed.fert = saver.ferti;
PlantWeed.procces = saver.pro;
PlantWeed.water = saver.Water;
PlantWeed.plantType = saver.PlantType;
LevelManager.IsReal = saver.isReal;
RestrictedZone.ObjOnField = saver.objOnField;
fStream.Close();
Objects = 0;
objects = GameObject.FindGameObjectsWithTag("WeedPlant");
for (int i = 0; i < objects.Length; i++)
{
Destroy(objects[i]);
Objects++;
}
}
}
}
}
[Serializable]
class SaveManager
{
public int money;
public bool farmBrought;
public bool bucket;
public bool shovel;
public int[] weedOnHand;
public int grams;
public int objects;
public float[] x;
public float[] y;
public float[] z;
public bool[] isGrown;
public int[] weedQuant;
public int[] pro;
public int[] Water;
public int[] PlantType;
public bool isReal;
public int[] ferti;
public int objOnField;
}
↧