Forum begins after the advertisement:


Part 11 ArgumentOutOfRangeException

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity Part 11 ArgumentOutOfRangeException

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #16722
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16723
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16724
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Well that change made the planting area stay on the digged state and always on harvest, I removed the return

    #16733
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    1
    ::

    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.
    #16734
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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
    #16735
    Jhan Khendrick Perez
    Level 5
    Participant
    #16736
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    I can share my project so that it can be thoroughly check

    #16742
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    1
    ::

    Did you leave the OnDestroy function in CropBehaviour? It should be removed.

    For good measure, you can paste your CropBehaviour script here.

    has upvoted this post.
    #16743
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    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);
    }

    }

    #16744
    Jonathan Teo
    Level 18
    Moderator
    Helpful?
    Up
    1
    ::

    Try implementing the fix in part 17 and see if it resolves your issue.

    has upvoted this post.
    #16745
    Jhan Khendrick Perez
    Level 5
    Participant
    Helpful?
    Up
    1
    ::

    Thank you, it works now

    has upvoted this post.
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: