Forum begins after the advertisement:
[Part 11] NullReferenceException: Object reference not set to an instance of an
Home › Forums › Video Game Tutorial Series › Creating a Farming RPG in Unity › [Part 11] NullReferenceException: Object reference not set to an instance of an
- This topic has 3 replies, 1 voice, and was last updated 3 days, 17 hours ago by Jhan Khendrick Perez.
-
AuthorPosts
-
December 15, 2024 at 5:22 pm #16786::
After finishing the Part 11 tutorial in which we are saving the Land and Crop Data so that whenever the player changes the scene the, planted crop and the status of the Land will be saved, however mine on the other hand is being NullReference after returning to the Planting Area
Scenario: Planting the Crop -> Goes to another scene -> Return to Planting Area scene -> Plant get wilted and NullReference Error
View post on imgur.com
December 15, 2024 at 5:23 pm #16787::The Error is being pointed in SeedData seedToGrow = (SeedData)NewInventoryManager.Instance.itemIndex.GetItemFromString(cropSave.seedToGrow);
SoilManager
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoilManager : MonoBehaviour { public static SoilManager Instance { get; private set; } public static Tuple<List<SoilSaveState>, List<CropSaveState>> urbanFarmData = null; //public GameObject plantingAreas; List<PottingSoil> soilPlots = new List<PottingSoil>(); List<SoilSaveState> soilData = new List<SoilSaveState>(); List<CropSaveState> cropData = new List<CropSaveState>(); private void Awake() { if (Instance != null && Instance != this) { Destroy(this); } else { Instance = this; } } private void OnEnable() { RegisterSoilPlots(); StartCoroutine(LoadFarmData()); } IEnumerator LoadFarmData() { yield return new WaitForEndOfFrame(); if (urbanFarmData != null) { //Load any save data ImportSoilData(urbanFarmData.Item1); ImportCropData(urbanFarmData.Item2); } } private void OnDestroy() { urbanFarmData = new Tuple<List<SoilSaveState>, List<CropSaveState>>(soilData, cropData); cropData.ForEach((CropSaveState crop) => { Debug.Log(crop.seedToGrow); }); } #region Register and Deregistering void RegisterSoilPlots() { foreach (Transform soilTransform in transform) { PottingSoil potSoil = soilTransform.GetComponent<PottingSoil>(); soilPlots.Add(potSoil); soilData.Add(new SoilSaveState()); potSoil.id = soilPlots.Count - 1; } /*foreach (GameObject plantingArea in plantingAreas) { foreach (Transform soilTransform in plantingArea.transform) { PottingSoil potSoil = soilTransform.GetComponent<PottingSoil>(); if (potSoil != null) { soilPlots.Add(potSoil); soilData.Add(new SoilSaveState()); potSoil.id = soilPlots.Count - 1; // Assign unique ID } } }*/ Debug.Log($"Registered {soilPlots.Count} soil plots."); } //Registers the crop onto the Instance public void RegisterCrop(int landID, SeedData seedToGrow, NewCropBehaviour.CropState cropState, int growth, int health) { cropData.Add(new CropSaveState(landID, seedToGrow.name, cropState, growth, health)); } public void DeregisterCrop(int soilID) { cropData.RemoveAll(x => x.soilID == soilID); } #endregion #region State Changes //Update the corresponding Soil Data on ever change to the soil's state public void OnSoilStateChange(int id, PottingSoil.SoilStatus soilStatus, GameTimeStamp lastWatered) { soilData[id] = new SoilSaveState(soilStatus, lastWatered); } public void OnCropStateChange(int soilID, NewCropBehaviour.CropState cropState, int growth, int health) { int cropIndex = cropData.FindIndex(x => x.soilID == soilID); if (cropIndex == -1) { Debug.LogError($"No crop found with landID {soilID}"); return; } string seedToGrow = cropData[cropIndex].seedToGrow; cropData[cropIndex] = new CropSaveState(soilID, seedToGrow, cropState, growth, health); } #endregion #region LoadingData public void ImportSoilData(List<SoilSaveState> soilDatasetToLoad) { for(int i = 0; i < soilDatasetToLoad.Count; i++) { SoilSaveState soilDataToLoad = soilDatasetToLoad[i]; soilPlots[i].LoadSoilData(soilDataToLoad.soilStatus, soilDataToLoad.lastWatered); } soilData = soilDatasetToLoad; } public void ImportCropData(List<CropSaveState> cropDatasetToLoad) { cropData = cropDatasetToLoad; foreach (CropSaveState cropSave in cropDatasetToLoad) { PottingSoil soilToPlant = soilPlots[cropSave.soilID]; NewCropBehaviour cropToPlant = soilToPlant.SpawnCrop(); Debug.Log(cropToPlant.gameObject); SeedData seedToGrow = (SeedData)NewInventoryManager.Instance.itemIndex.GetItemFromString(cropSave.seedToGrow); cropToPlant.LoadCrop(cropSave.soilID, seedToGrow, cropSave.cropState, cropSave.growth, cropSave.health); } } #endregion }
December 15, 2024 at 6:01 pm #16789December 15, 2024 at 6:01 pm #16790 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: