Forum begins after the advertisement:
Part 11 ArgumentOutOfRangeException
Home › Forums › Video Game Tutorial Series › Creating a Farming RPG in Unity › Part 11 ArgumentOutOfRangeException
- This topic has 10 replies, 2 voices, and was last updated 6 days, 20 hours ago by Jhan Khendrick Perez.
-
AuthorPosts
-
December 10, 2024 at 6:57 pm #16722::
Good Evening, I am encountering an error regarding the Part 11 on the part of ArgumentOutOfRangeException, following the tutorial and comparing to the final product, still shows the same error. Do I need to complete the Scene Transition tutorial for this to work?
View post on imgur.com
Thank you
December 10, 2024 at 10:18 pm #16723::I am not sure if this is a good solution but I saw a comment on the Video that adding return will solve the problem, it works, but I would like to ask for your opinion on this one.
if(growth >= maxGrowth && cropState == CropState.Seedling) { SwitchState(CropState.Harvestable); return; }
Thank you
December 10, 2024 at 11:49 pm #16724::Well that change made the planting area stay on the digged state and always on harvest, I removed the return
December 11, 2024 at 7:28 am #16733::Edit your code to be like this and let me know the output:
public void OnCropStateChange(int landID, CropBehaviour.CropState cropState, int growth, int health) { int cropIndex = cropData.FindIndex(x => x.landID == landID); if (cropIndex == -1) { Debug.LogError($"No crop found with landID {landID}"); return; } string seedToGrow = cropData[cropIndex].seedToGrow; cropData[cropIndex] = new CropSaveState(landID, seedToGrow, cropState, growth, health); }
has upvoted this post. December 11, 2024 at 9:18 am #16734::Good Morning, it showed the Log Error, my land ID 1 is the Pot, description also posted within the each image
View post on imgur.com
December 11, 2024 at 9:18 am #16735December 11, 2024 at 12:32 pm #16736December 12, 2024 at 5:18 am #16742December 12, 2024 at 9:59 am #16743::Good Morning here is the CropBehaviour script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class NewCropBehaviour : MonoBehaviour { int soilID;
SeedData seedToGrow; [Header("Plant Stages")] public GameObject seed; public GameObject wilted; private GameObject seedling; private GameObject harvestable; int growth; int maxGrowth; int maxHealth = GameTimeStamp.HoursToMinutes(48); int health; public enum CropState { Seed, Seedling, Harvestable, Wilted } public CropState cropState; public void Plant(int soilID, SeedData seedToGrow) { LoadCrop(soilID, seedToGrow, CropState.Seed, 0, 0); SoilManager.Instance.RegisterCrop(soilID, seedToGrow, cropState, growth, health); } public void LoadCrop(int soilID, SeedData seedToGrow, CropState cropState, int growth, int health) { this.soilID = soilID; this.seedToGrow = seedToGrow; seedling = Instantiate(seedToGrow.seedling, transform); ItemData cropToYield = seedToGrow.cropToYield; harvestable = Instantiate(cropToYield.gameModel, transform); //Convert Days to Grow into Hours int hoursToGrow = GameTimeStamp.DaysToHours(seedToGrow.daysToGrow); //Convert to Minutes, since tha plant grows by the minutes maxGrowth = GameTimeStamp.HoursToMinutes(hoursToGrow); this.growth = growth; this.health = health; //Check if its regrowable if (seedToGrow.regrowable) { RegrowableHarvestBehaviour regrowableHarvest = harvestable.GetComponent<RegrowableHarvestBehaviour>(); regrowableHarvest.SetParent(this); } //Initial state to seed SwitchState(cropState); } public void Grow() { growth++; if(health < maxHealth) { health++; } //The seed will sprout into a seedling when growth is at 50% if(growth >= maxGrowth / 2 && cropState == CropState.Seed) { SwitchState(CropState.Seedling); } //Grow to seedling if(growth >= maxGrowth && cropState == CropState.Seedling) { SwitchState(CropState.Harvestable); //return; } SoilManager.Instance.OnCropStateChange(soilID, cropState, growth, health); } public void Wither() { health--; if(health <= 0 && cropState != CropState.Seed) { SwitchState(CropState.Wilted); } SoilManager.Instance.OnCropStateChange(soilID, cropState, growth, health); } void SwitchState(CropState stateToSwitch) { seed.SetActive(true); seedling.SetActive(false); harvestable.SetActive(false); wilted.SetActive(false); switch (stateToSwitch) { case CropState.Seed: seed.SetActive(true); break; case CropState.Seedling: seedling.SetActive(true); health = maxHealth; break; case CropState.Harvestable: harvestable.SetActive(true); //If the seed is not regrowable, detach the harvestable from this gameobject and destroy it. if (!seedToGrow.regrowable) { //Unparenting harvestable.transform.parent = null; RemoveCrop(); } break; case CropState.Wilted: wilted.SetActive(true); break; } cropState = stateToSwitch; } public void RemoveCrop() { SoilManager.Instance.DeregisterCrop(soilID); Destroy(gameObject); } public void Regrow() { //Reset Growth //Get the regrowth time in hours int hoursToRegrow = GameTimeStamp.DaysToHours(seedToGrow.daysToGrow); growth = maxGrowth - GameTimeStamp.HoursToMinutes(hoursToRegrow); SwitchState(CropState.Seedling); }
}
December 12, 2024 at 2:23 pm #16744December 12, 2024 at 3:01 pm #16745 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: