Forum begins after the advertisement:


[General] Null reference error in PlayerInventory

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [General] Null reference error in PlayerInventory

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15105
    Jason Hahne
    Participant
    Helpful?
    Up
    0
    ::

    I’m getting NullReference error in PlayerInventory script. I’m getting error on Type weaponType = Type.GetType(data.behaviour);. I’m not sure why the error is happening. I would appreciate it if anyone can help me with this problem.

    public int Add(WeaponData data)
    {
        int slotNum = -1;
    
        for (int i = 0; i < weaponSlots.Capacity; i++)
        {
            if (weaponSlots[i].IsEmpty())
            {
                slotNum = i;
                break;
            }
        }
    
        if (slotNum < 0)
        {
            return slotNum;
        }
    
        Type weaponType = Type.GetType(data.behaviour);
    
        if (weaponType != null)
        {
            GameObject go = new GameObject(data.baseStats.name + " Controller");
            WeaponBase spawnedWeapon = (WeaponBase)go.AddComponent(weaponType);
            spawnedWeapon.transform.SetParent(transform);
            spawnedWeapon.transform.localEulerAngles = Vector2.zero;
            spawnedWeapon.Initialize(data);
            spawnedWeapon.OnEquip();
    
            weaponSlots[slotNum].Assign(spawnedWeapon);
    
            if (GameManager.instance != null && GameManager.instance.choosingUpgrade)
            {
                GameManager.instance.EndLevelUp();
            }
            return slotNum;
        }
    
        return -1;
    }
    #15106
    Jason Hahne
    Participant
    Helpful?
    Up
    0
    ::

    I solved the issue. I forgot to assign weapondata to player scriptable object. :D

    #15107
    Terence
    Keymaster
    Helpful?
    Up
    0
    ::

    Glad you fixed your issue Jason. Congrats!

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

Go to Login Page →


Advertisement below: