Forum begins after the advertisement:
[Part 8/11] Need help solving InventoryManager.cs From part 8
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 8/11] Need help solving InventoryManager.cs From part 8
- This topic has 11 replies, 2 voices, and was last updated 2 weeks, 3 days ago by Terence.
-
AuthorPosts
-
November 24, 2024 at 9:00 am #16507::
I’ve been trying to figure out what’s wrong with my code from part 8 tutorial, everything was going smoothly until the level system of the weapons and items are not working properly. They do level up to level 2 but instantly overlapped by the level 1 gameobject. Please help! T_T
using System.Collections; using System.Collections.Generic; using UnityEngine; public class InventoryManager : MonoBehaviour { public List<WeaponController> weaponSlots = new List<WeaponController>(6); public int[] weaponLevels = new int[6]; public List<PassiveItem> passiveItemSlots = new List<PassiveItem>(6); public int[] passiveItemLevels = new int[6]; public void AddWeapon(int slotIndex, WeaponController weapon) //Add a weapon to a specific slot { weaponSlots[slotIndex] = weapon; weaponLevels[slotIndex] = weapon.weaponData.Level; } public void AddPassiveItem(int slotIndex, PassiveItem passiveItem) //Add a passive item to a specific slot { passiveItemSlots[slotIndex] = passiveItem; passiveItemLevels[slotIndex] = passiveItem.passiveItemData.Level; } public void LevelUpWeapon(int slotIndex) { if(weaponSlots.Count > slotIndex) { WeaponController weapon = weaponSlots[slotIndex]; if (!weapon.weaponData.NextLevelPrefab) { Debug.LogError("NO NEXT LEVEL FOR " + weapon.name); //Checks if there is a next level for the weapon return; } GameObject upgradedWeapon = Instantiate(weapon.weaponData.NextLevelPrefab, transform.position, Quaternion.identity); upgradedWeapon.transform.SetParent(transform); //Sets the weapon to be a child of the player AddWeapon(slotIndex, upgradedWeapon.GetComponent<WeaponController>()); Destroy(weapon.gameObject); //Destroys previous weapon from the scene to prevent overlapping weaponLevels[slotIndex] = upgradedWeapon.GetComponent<WeaponController>().weaponData.Level; //Makes sure we have the correct weapon level } } public void LevelUpPassiveItem(int slotIndex) { if(passiveItemSlots.Count > slotIndex) { PassiveItem passiveItem = passiveItemSlots[slotIndex]; if (!passiveItem.passiveItemData.NextLevelPrefab) { Debug.LogError("NO NEXT LEVEL FOR " + passiveItem.name); //Checks if there is a next level for the passive item return; } GameObject upgradedPassiveItem = Instantiate(passiveItem.passiveItemData.NextLevelPrefab, transform.position, Quaternion.identity); upgradedPassiveItem.transform.SetParent(transform); //Sets the passive item to be a child of the player AddPassiveItem(slotIndex, upgradedPassiveItem.GetComponent<PassiveItem>()); Destroy(passiveItem.gameObject); //Destroys previous passive item from the scene to prevent overlapping passiveItemLevels[slotIndex] = upgradedPassiveItem.GetComponent<PassiveItem>().passiveItemData.Level; //Makes sure we have the correct passive item level } } }
November 24, 2024 at 3:04 pm #16512November 24, 2024 at 3:05 pm #16513November 24, 2024 at 3:12 pm #16516November 24, 2024 at 4:38 pm #16518::Are the names of the levelled up prefabs correct before they get replaced? How about in the hierarchy? Do the LV2 weapons get spawned correctly and attached to your character? Or do they get deleted as well?
November 24, 2024 at 4:41 pm #16519::They get deleted as well but all the weapons work correctly. I also double check everything if the names are correct and the scriptable objects are put on the corrent level prefabs. It’s kind of like the prefabs of the level 2 weapons and items are trying replace the level 1 prefabs on the list but gets instantly overlapped by the level 1 prefab I don’t know if the problem is within the inventorysystem script or the level script… But i can send any of the scripts you’d like for me to send, or even send the github file for you to test.
November 24, 2024 at 4:55 pm #16520::After the weapon gets replaced in the weapon slot variable, can you click it in the array and see which file it leads you to?
November 24, 2024 at 5:08 pm #16522::I removed the Destroy line to test where it leads to and here’s the only thing it lead it: https://imgur.com/a/DhZ1hWO
November 24, 2024 at 5:46 pm #16523::Here is the github file if you want to test it out if you want to see the problem and make it easier for you to understand the error: https://github.com/KenChiimken/AMG-Finals-WIP
November 24, 2024 at 7:09 pm #16527November 24, 2024 at 7:24 pm #16528::As for the weapons, I tried them and they don’t apply the upgrades just like the wings item does and they’re still stuck on level 1 level value and level 1 stats.
November 24, 2024 at 10:01 pm #16533::There are a ton of null reference exceptions in your project. Your weapon prefabs do not have scriptable objects assigned. Try assigning them and see whether it fixes the issue.
If it still doesn’t fix the issue, you can either:
- Check if there are any warnings / errors in the console, and fix these errors that arise.
- Get a Patreon subscription to download our project files, or compare your code to the ones in the article.
Here’s a 1 minute video on how to fix null reference exceptions (that your current project has a lot of).
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: