Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Arcturus125 · Nov 11, 2020 at 12:56 AM · c#build-errorserializationscriptableobjectinheritance

ScriptableObject error with Inheritance

here is the error i recieve: alt text

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):

 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;
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Arcturus125 · Nov 12, 2020 at 12:36 AM

I made an identical post here once this post went stale: https://stackoverflow.com/questions/64780739/scriptableobject-build-error-with-inheritance-but-runs-fine-in-unity-editor


it turns out I was running some old code by accident. the constructors in both Item and Weapon were completely unnecessary.

in the other thread, I found out that constructors don't work well with scripts inheriting ScriptableObject, because unity handles the construction of a scriptable object for you... my constructors managed to interfere with that. luckily I didn't need the constructors anyway and left them there by accident. after removing them, the code worked fine,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jackmw94 · Nov 11, 2020 at 01:52 AM

Both ScriptableObjects and MonoBehaviours are required to exist within files that have the filename matching the class name. Given that both of these look like they're in a single file, I'm guessing there's an issue with one of them.

This took me ages to debug the first time I ran into this!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arcturus125 · Nov 11, 2020 at 02:32 AM 0
Share

I tried putting the weapon class in a different file. and nothing. it still has the same error message. Any other ideas?

avatar image jackmw94 Arcturus125 · Nov 11, 2020 at 02:49 AM 0
Share

Damn. Sanity check that the file names are definitely Item and Weapon, but I do believe you ;)

It may be because dictionaries aren't serializable by unity? I'd have expected it to warn you a little better than that but you might run into problems there.

If that is the case then you might have to resort to having a list of pairs then creating the dictionary during initialisation. Alternatively Odin Inspector handles dictionary serialization and is amazing! I'll let you see whether that helps before continuing..

avatar image Arcturus125 jackmw94 · Nov 11, 2020 at 03:00 AM 0
Share

I tried removing the dictionary since I didn't really utilise it much. same error

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

144 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

An OS design issue: File types associated with their appropriate programs 1 Answer

Calling a derived class (Serialized in a Scriptable Object) returns the super class. 1 Answer

Copy reference from one component to another derived component., 2 Answers

Saving UnityEngine.Object into ScriptableObject 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges