Forum begins after the advertisement:
[Part 12] ArgumentOutOfRange error when upgrading garlic after evolved Knife
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 12] ArgumentOutOfRange error when upgrading garlic after evolved Knife
- This topic has 4 replies, 2 voices, and was last updated 1 week ago by Terence.
-
AuthorPosts
-
November 13, 2024 at 10:03 am #16315::
Hello,
First thing first, thanks a lot for your amazing work, it’s really a pleasure to follow your step by step tutorial, really good explained, and easy to follow.
I’m sorry, but i can’t resolve this by myself:
Step 1 = select Garlic Character
Step 2 = upgrade Knife lvl 2 and collect Spinach
Step 3 = loot the treasure chest
Step 4 = upgrade Garlic lvl 2 and this is what happen(My garlic has 3 level)
https://postimg.cc/DSj3RW0y (problem to add an image)
November 13, 2024 at 8:33 pm #16320::Can you paste your
InventoryManager
code here as well?An
ArgumentOutOfRangeException
happens when you try to access an array value that is larger than the size of the array itself. For e.g., if an array has 2 items, and you try to accessarr[6]
, it will cause this error.November 13, 2024 at 8:43 pm #16321::Hello :)
I’ve made some changes (if i chose Garlic Character, i can have the evolved knife and it is well replace)
The problem come in all case if i want upgrade a weapon after knife evolution.i’m trying to follow the Part 15 who changes everythings, and it’s 20 minuts of video for an hour of comprehension, several issues incoming and i broke my eyes comparind your code and mine, but it’s fine, i do my best :)
(i have other problems and no solution for moment health bar decrease is ok, but not increase, it’s not killing the following steps, so i don’t care about it for moment oO )
Thanks for all, really, i wish i can help you back soon (business is business and you deserve it)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; public class InventoryManager : MonoBehaviour{ public List<WeaponController> weaponSlots = new List<WeaponController>(6); public int[] weaponLevels = new int[6]; public List<Image> weaponUISlots = new List<Image>(6); public List<PassiveItem> passiveItemSlots = new List<PassiveItem>(6); public int[] passiveItemLevels = new int[6]; public List<Image> passiveItemUISlots = new List<Image>(6); // ---- Upgrade managment ----// [System.Serializable] public class WeaponUpgrade{ public int weaponUpgradeIndex; public GameObject initialWeapon; public WeaponScriptableObject weaponData; } [System.Serializable] public class PassiveItemUpgrade{ public int passiveItemUpgradeIndex; public GameObject initialPassiveItem; public PassiveItemScriptableObject passiveItemData; } [System.Serializable] public class UpgradeUI{ public TMP_Text upgradeNameDisplay; public TMP_Text upgradeDescriptionDisplay; public Image upgradeIcon; public Button upgradeButton; } public List<WeaponUpgrade> weaponUpgradeOptions = new List<WeaponUpgrade>(); public List<PassiveItemUpgrade> passiveItemUpgradeOptions = new List<PassiveItemUpgrade>(); public List<UpgradeUI> upgradeUIOptions = new List<UpgradeUI>(); public List<WeaponEvolutionBlueprint> weaponEvolutions = new List<WeaponEvolutionBlueprint>(); PlayerStats player; void Start(){ player = GetComponent<PlayerStats>(); } // ---- Upgrade managment ----// public void AddWeapon(int slotIndex, WeaponController weapon){ // Add a weapon to a specific slot weaponSlots[slotIndex] = weapon; weaponLevels[slotIndex] = weapon.weaponData.Level; weaponUISlots[slotIndex].enabled = true; weaponUISlots[slotIndex].sprite = weapon.weaponData.Icon; if(GameManager.instance != null && GameManager.instance.choosingUpgrade){ GameManager.instance.EndLevelUp(); } } public void AddPassiveItem(int slotIndex, PassiveItem passiveItem){ // Add a passive item to a specific slot passiveItemSlots[slotIndex] = passiveItem; passiveItemLevels[slotIndex] = passiveItem.passiveItemData.Level; passiveItemUISlots[slotIndex].enabled = true; passiveItemUISlots[slotIndex].sprite = passiveItem.passiveItemData.Icon; if(GameManager.instance != null && GameManager.instance.choosingUpgrade){ GameManager.instance.EndLevelUp(); } } public void LevelUpWeapon(int slotIndex, int upgradeIndex){ if(weaponSlots.Count > slotIndex){ WeaponController weapon = weaponSlots[slotIndex]; if(!weapon.weaponData.NextLevelPrefab){ // Check if there is a next level for the current weapon Debug.LogError("NO NEXT LEVEL FOR " + weapon.name); return; } GameObject upgradedWeapon = Instantiate(weapon.weaponData.NextLevelPrefab, transform.position, Quaternion.identity); upgradedWeapon.transform.SetParent(transform); // Set the weapon to be a child of the player AddWeapon(slotIndex, upgradedWeapon.GetComponent<WeaponController>()); Destroy(weapon.gameObject); weaponLevels[slotIndex] = upgradedWeapon.GetComponent<WeaponController>().weaponData.Level; // To make sure we have the correct weapon level weaponUpgradeOptions[upgradeIndex].weaponData = upgradedWeapon.GetComponent<WeaponController>().weaponData; if(GameManager.instance != null && GameManager.instance.choosingUpgrade){ GameManager.instance.EndLevelUp(); } } } public void LevelUpPassiveItem(int slotIndex, int upgradeIndex){ if(passiveItemSlots.Count > slotIndex){ PassiveItem passiveItem = passiveItemSlots[slotIndex]; if(!passiveItem.passiveItemData.NextLevelPrefab){ Debug.LogError("NO NEXT LEVEL FOR " + passiveItem.name); return; } GameObject upgradedPassiveItem = Instantiate(passiveItem.passiveItemData.NextLevelPrefab, transform.position, Quaternion.identity); upgradedPassiveItem.transform.SetParent(transform); AddPassiveItem(slotIndex, upgradedPassiveItem.GetComponent<PassiveItem>()); Destroy(passiveItem.gameObject); passiveItemLevels[slotIndex] = upgradedPassiveItem.GetComponent<PassiveItem>().passiveItemData.Level; passiveItemUpgradeOptions[upgradeIndex].passiveItemData = upgradedPassiveItem.GetComponent<PassiveItem>().passiveItemData; if(GameManager.instance != null && GameManager.instance.choosingUpgrade){ GameManager.instance.EndLevelUp(); } } } void ApplyUpgradeOptions(){ List<WeaponUpgrade> availableWeaponUpgrades = new List<WeaponUpgrade>(weaponUpgradeOptions); List<PassiveItemUpgrade> availablePassiveItemUpgrades = new List<PassiveItemUpgrade>(passiveItemUpgradeOptions); foreach(var upgradeOption in upgradeUIOptions){ // No more upgrade or item to unlock if(availableWeaponUpgrades.Count == 0 && availablePassiveItemUpgrades.Count == 0){ return; } int upgradeType; if(availableWeaponUpgrades.Count == 0){ // no more weapon upgrade or item to unlock upgradeType = 2; }else if(availablePassiveItemUpgrades.Count == 0){ // no more passive item upgrade or item to unlock upgradeType = 1; }else{ upgradeType = Random.Range(1, 3); // Choose between weapon and passive item } if(upgradeType == 1){ WeaponUpgrade chosenWeaponUpgrade = availableWeaponUpgrades[Random.Range(0, availableWeaponUpgrades.Count)]; availableWeaponUpgrades.Remove(chosenWeaponUpgrade); if(chosenWeaponUpgrade != null){ EnableUpgradeUI(upgradeOption); bool newWeapon = false; for(int i = 0; i < weaponSlots.Count; i++){ if(weaponSlots[i] != null && weaponSlots[i].weaponData == chosenWeaponUpgrade.weaponData){ newWeapon = false; if(!newWeapon){ // Limit the upgrade if there is no more possibilities if(!chosenWeaponUpgrade.weaponData.NextLevelPrefab){ DisableUpgradeUI(upgradeOption); break; } upgradeOption.upgradeButton.onClick.AddListener(() => LevelUpWeapon(i, chosenWeaponUpgrade.weaponUpgradeIndex)); // Apply button functionality // Set the description and the name to be that of the next level upgradeOption.upgradeDescriptionDisplay.text = chosenWeaponUpgrade.weaponData.NextLevelPrefab.GetComponent<WeaponController>().weaponData.Description; upgradeOption.upgradeNameDisplay.text = chosenWeaponUpgrade.weaponData.NextLevelPrefab.GetComponent<WeaponController>().weaponData.Name; } break; }else{ newWeapon = true; } } if(newWeapon){ // Spawn a new weapon upgradeOption.upgradeButton.onClick.AddListener(() => player.SpawnWeapon(chosenWeaponUpgrade.initialWeapon)); // Apply button functionality // Apply initial description and name upgradeOption.upgradeDescriptionDisplay.text = chosenWeaponUpgrade.weaponData.Description; upgradeOption.upgradeNameDisplay.text = chosenWeaponUpgrade.weaponData.Name; } upgradeOption.upgradeIcon.sprite = chosenWeaponUpgrade.weaponData.Icon; } }else if(upgradeType == 2){ PassiveItemUpgrade chosenPassiveItemUpgrade = availablePassiveItemUpgrades[Random.Range(0, availablePassiveItemUpgrades.Count)]; availablePassiveItemUpgrades.Remove(chosenPassiveItemUpgrade); if(chosenPassiveItemUpgrade != null){ EnableUpgradeUI(upgradeOption); bool newPassiveItem = false; for(int i = 0; i < passiveItemSlots.Count; i++){ if(passiveItemSlots[i] != null && passiveItemSlots[i].passiveItemData == chosenPassiveItemUpgrade.passiveItemData){ newPassiveItem = false; if(!newPassiveItem){ if(!chosenPassiveItemUpgrade.passiveItemData.NextLevelPrefab){ DisableUpgradeUI(upgradeOption); break; } upgradeOption.upgradeButton.onClick.AddListener(() => LevelUpPassiveItem(i, chosenPassiveItemUpgrade.passiveItemUpgradeIndex)); upgradeOption.upgradeDescriptionDisplay.text = chosenPassiveItemUpgrade.passiveItemData.NextLevelPrefab.GetComponent<PassiveItem>().passiveItemData.Description; upgradeOption.upgradeNameDisplay.text = chosenPassiveItemUpgrade.passiveItemData.NextLevelPrefab.GetComponent<PassiveItem>().passiveItemData.Name; } break; }else{ newPassiveItem = true; } } if(newPassiveItem){ upgradeOption.upgradeButton.onClick.AddListener(() => player.SpawnPassiveItem(chosenPassiveItemUpgrade.initialPassiveItem)); upgradeOption.upgradeDescriptionDisplay.text = chosenPassiveItemUpgrade.passiveItemData.Description; upgradeOption.upgradeNameDisplay.text = chosenPassiveItemUpgrade.passiveItemData.Name; } upgradeOption.upgradeIcon.sprite = chosenPassiveItemUpgrade.passiveItemData.Icon; } } } } void RemoveUpgradeOptions(){ foreach(var upgradeOption in upgradeUIOptions){ upgradeOption.upgradeButton.onClick.RemoveAllListeners(); DisableUpgradeUI(upgradeOption); } } public void RemoveAndApplyUpgrades(){ RemoveUpgradeOptions(); ApplyUpgradeOptions(); } void DisableUpgradeUI(UpgradeUI ui){ ui.upgradeNameDisplay.transform.parent.gameObject.SetActive(false); } void EnableUpgradeUI(UpgradeUI ui){ ui.upgradeNameDisplay.transform.parent.gameObject.SetActive(true); } public List<WeaponEvolutionBlueprint> GetPossibleEvolutions(){ // List to store possible weapon evolutions var possibleEvolutions = new List<WeaponEvolutionBlueprint>(); foreach(WeaponController weapon in weaponSlots){ if(weapon != null){ foreach(PassiveItem catalyst in passiveItemSlots){ if(catalyst != null){ foreach(WeaponEvolutionBlueprint evolution in weaponEvolutions){ if(weapon.weaponData.Level >= evolution.baseWeaponData.Level && catalyst.passiveItemData.Level >= evolution.catalystPassiveItemData.Level){ possibleEvolutions.Add(evolution); } } } } } } // Return list of possible evolutions return possibleEvolutions; } public void EvolveWeapon(WeaponEvolutionBlueprint evolution){ for(int weaponSlotIndex = 0; weaponSlotIndex < weaponSlots.Count; weaponSlotIndex++){ WeaponController weapon = weaponSlots[weaponSlotIndex]; if(!weapon){continue;} // We check that the weapon matches the one to evolve if(weapon.weaponData != evolution.baseWeaponData) continue; for(int catalystSlotIndex = 0; catalystSlotIndex < passiveItemSlots.Count; catalystSlotIndex++){ PassiveItem catalyst = passiveItemSlots[catalystSlotIndex]; if(!catalyst){continue;} if(catalyst.passiveItemData == evolution.catalystPassiveItemData && weapon.weaponData.Level >= evolution.baseWeaponData.Level && catalyst.passiveItemData.Level >= evolution.catalystPassiveItemData.Level) { // Creation and positioning of the evolved weapon GameObject evolvedWeapon = Instantiate(evolution.evolvedWeapon, transform.position, Quaternion.identity); WeaponController evolvedWeaponController = evolvedWeapon.GetComponent<WeaponController>(); evolvedWeapon.transform.SetParent(transform); // Attach the weapon to the character // Replace the existing weapon in the slot AddWeapon(weaponSlotIndex, evolvedWeaponController); Destroy(weapon.gameObject); // Update the level and icon weaponLevels[weaponSlotIndex] = evolvedWeaponController.weaponData.Level; weaponUISlots[weaponSlotIndex].sprite = evolvedWeaponController.weaponData.Icon; // Update upgrade options int indexToRemove = evolvedWeaponController.weaponData.EvolvedUpgradeToRemove; // Check if the index to remove is valid (non-negative and within the list's range) if(indexToRemove >= 0 && indexToRemove < weaponUpgradeOptions.Count){ weaponUpgradeOptions.RemoveAt(indexToRemove); }else{ Debug.LogWarning("Invalid index for upgrade removal: " + indexToRemove); } Debug.LogWarning("Evolved"); return; } } } } }
has upvoted this post. November 14, 2024 at 1:21 am #16324::I think I was on the verge of finding it when I saw the -1 in part 15, but I had no idea how to integrate it. I think it’s unnecessary to fix this bug, because from part 15, things really start to get serious. I admit the system was rather cumbersome to manage. I’m not spoiling the rest, but the wave management also seems to have a problem in my opinion, because the way the number of mobs is handled is either global or really works randomly (I think).
has upvoted this post. November 14, 2024 at 9:12 am #16327::Thanks for all, really, i wish i can help you back soon (business is business and you deserve it)
Thank you my friend. I’m glad I can help.
I’m not spoiling the rest, but the wave management also seems to have a problem in my opinion, because the way the number of mobs is handled is either global or really works randomly (I think).
You’re right, that’s why I redesigned the enemy spawning system in Part 21, to make it more stable: https://blog.terresquall.com/2024/07/creating-a-rogue-like-vampire-survivors-part-21/
I think I was on the verge of finding it when I saw the -1 in part 15, but I had no idea how to integrate it.
Let me know if you need more help fixing this.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: