A scripted Object has a different layout when loading: scriptable objects and inheritance

here is the error i recieve:

the error is on this line.:

if (pItem.isStackable)
{
}

I should note that the error only occurs when I build the game. It does not occur when I run the game in the editor.

I believe the error comes from trying to give the player a weapon in their inventory.

because this runs fine:

Player.playerInv.AddItem(apple);

and this doesn’t:

 Player.playerInv.AddItem(sword2);

the only difference is apple is an item and Sword2 is a weapon…
I have reason to believe that the build is expecting an Item to be loaded and not a weapon. (Weapon is a child of item. item inherits ScriptableObject).

here is the code for weapons and items (sorry for large file):

// Copyright(c) 2020 arcturus125 & StrangeDevTeam
// Free to use and modify as you please, Not to be published, distributed, licenced or sold without permission from StrangeDevTeam
// Requests for the above to be made here: https://www.reddit.com/r/StrangeDev/

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[CreateAssetMenu(fileName = "Item", menuName = "StrangeEngine/Item", order = 2)]
public class Item : ScriptableObject
{
    public enum ItemType
    {
        Item = 1,
        Skill = 2,
        Equipment = 3
    }
    public ItemType type;
    public int ID;
    public string itemName;
    public string info;
    public int worth;
    public bool isStackable = false;
    //public Sprite icon;
    public Dictionary<string, int> itemStats = new Dictionary<string, int>(); // "stats" that come in pairs, a name (string) and a number. for example ("weight", 22)

    public Item(ItemType pType, int pID, string pName, string pInfo, int pWorth, Dictionary<string,int> pStats)
    {
        type = pType;
        ID = pID;
        itemName = pName;
        info = pInfo;
        worth = pWorth;
        itemStats = pStats;
    }
    public Item(ItemType pType, int pID, string pName, string pInfo, int pWorth, Dictionary<string, int> pStats, bool pIsStackable)
    {
        type = pType;
        ID = pID;
        itemName = pName;
        info = pInfo;
        worth = pWorth;
        itemStats = pStats;
        isStackable = pIsStackable;
    }
}

[CreateAssetMenu(fileName = "Item", menuName = "StrangeEngine/Weapon", order = 3)]
public class Weapon : Item
{
    public int Damage;
    public GameObject WeaponModelPrefab;


    public Weapon(int pID, string pName, string pInfo, int pWorth, Dictionary<string, int> pStatsfloat, int pDamage, GameObject pWeaponPrefab) : base(ItemType.Equipment,pID, pName, pInfo,pWorth,pStatsfloat)
    {
        Damage = pDamage;
        WeaponModelPrefab = pWeaponPrefab;
    }
}

Hi Arcturus,

have you found a solution for your problem?

Best,
rehawk

I have the same problem on my Android phone, occasionally.,

First error, I’ve seen that before #indef Unity_Editor , is usually in the parent class and sets different setups for the class/script to work, depending if it’s in editor or actual game. I personally have not played with those yet, but I do know there should be more #if's to reference builds. Second error, I think is because you say Add.Item(Sword), instead of Add.Weapon(Sword) , which the 2 references to Items is a tad confusing with one not having the isStackable trait, and the second one does. There should only be 1 reference, and just a bool change to if it is stackable or not. Third error is best guess, that it’s confused on loading weapon as an Item to Inventory, therefor cannot reference the object properly, throwing the null error?

This problem should be seen as a dark bug below the unity shine surface.

I’ve spent a whole day to find out this thing.

If you just like me checked everything and there is’nt any UNITY_EDITOR stuff left in the code.
And maybe you just use the Resources.LoadAll(string.Empty) to find out the specified assets you want to be load at runtime.
That hits the problem.

DO NOT use the string.Empty as the parameter. Use a very specified path to locate your assets.

eg: Resources.LoadAll<Asset>("Game/Assets");

Otherwise it will fail after packaging the game.

It seems this empty parameter force unity to load some unknown objects that wasn’t made by you.