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

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #16507
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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
            }
        }
    }
    #16512
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Can you show a video recording of the issue?

    #16513
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    Yes, sure! Give me a minute to record it.

    #16516
    KenChii
    Level 1
    Participant
    #16518
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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?

    #16519
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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.

    #16520
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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?

    #16522
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16523
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16527
    KenChii
    Level 1
    Participant
    #16528
    KenChii
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    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.

    #16533
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    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:

    1. Check if there are any warnings / errors in the console, and fix these errors that arise.
    2. 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).

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

Go to Login Page →


Advertisement below: