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?

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/lxigZzF”><a href=”https://imgur.com/a/lxigZzF” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    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.

    <code>if(growth &gt;= maxGrowth &amp;&amp; cropState == CropState.Seedling)
    {
        SwitchState(CropState.Harvestable);
        return;
    }</code>

    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;
    }</p>
    

    string seedToGrow = cropData[cropIndex].seedToGrow; cropData[cropIndex] = new CropSaveState(landID, seedToGrow, cropState, growth, health);

    
    <p>}
    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

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/DdBf2Ba”><a href=”https://imgur.com/a/DdBf2Ba” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

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

    <blockquote class=”imgur-embed-pub” lang=”en” data-id=”a/DdBf2Ba”><a href=”https://imgur.com/a/DdBf2Ba” target=”_blank”>View post on imgur.com</a></blockquote><script async src=”//s.imgur.com/min/embed.js” charset=”utf-8″></script>

    #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;

    <code>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&lt;RegrowableHarvestBehaviour&gt;();
            regrowableHarvest.SetParent(this);
        }
    
        //Initial state to seed
        SwitchState(cropState);
    }
    
    public void Grow()
    {
        growth++;
    
        if(health &lt; maxHealth)
        {
            health++;
        }
    
        //The seed will sprout into a seedling when growth is at 50%
        if(growth &gt;= maxGrowth / 2 &amp;&amp; cropState == CropState.Seed)
        {
            SwitchState(CropState.Seedling);
        }
    
        //Grow to seedling
        if(growth &gt;= maxGrowth &amp;&amp; cropState == CropState.Seedling)
        {
            SwitchState(CropState.Harvestable);
            //return;
        }
    
        SoilManager.Instance.OnCropStateChange(soilID, cropState, growth, health);
    }
    
    public void Wither()
    {
        health--;
    
        if(health &lt;= 0 &amp;&amp; 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);
    }</code>

    }

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

    Try implementing the fix in part 17 and see if it resolves your issue. <iframe title=”Creating a Farming RPG (like Harvest Moon) in Unity — Part 17: Bug Fixing and Inventory Tab Panels” width=”1000″ height=”563″ src=”https://www.youtube.com/embed/sxtmJx_UJ64?start=139&feature=oembed” frameborder=”0″ allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share” referrerpolicy=”strict-origin-when-cross-origin” allowfullscreen></iframe>

    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: