❔ ✅ Serializable attribute in Unity

I am creating a class selection menu right now for game character. I have created two types:
[System.Serializable]
public class CharacterClass : ScriptableObject{
public string characterClassName;
public Sprite characterClassSprite;
}
[CreateAssetMenu]
public class CharacterClassSelection : ScriptableObject{
public CharacterClass[] characterClasses;
}
[System.Serializable]
public class CharacterClass : ScriptableObject{
public string characterClassName;
public Sprite characterClassSprite;
}
[CreateAssetMenu]
public class CharacterClassSelection : ScriptableObject{
public CharacterClass[] characterClasses;
}
now the Create Asset Menu is working, when i create a class of CharacterClassSelection the characterClasses array can be assigned via the Inspector. But serialization of CharacterClass seems to fail, when i add object i dont see fields characterClassName and characterClassSprite 😦
15 Replies
ero
ero2y ago
try putting the attribute on the fields directly
Florian Voß
Florian VoßOP2y ago
@Ero Assets\Scripts\ClassSelection\CharacterClass.cs(8,6): error CS0592: Attribute 'Serializable' is not valid on this declaration type. It is only valid on 'class, struct, enum, delegate' declarations. I can't put the attribute on the fields I have no idea what to try next or why it isnt working
ero
ero2y ago
[SerializeField]?
Florian Voß
Florian VoßOP2y ago
oh
Florian Voß
Florian VoßOP2y ago
same result
Florian Voß
Florian VoßOP2y ago
@Ero
ero
ero2y ago
no clue
Florian Voß
Florian VoßOP2y ago
this is so weird i do exactly what tutorial tells me to do, maybe different versions cause this would it be the Unity version or the C# version I care about?
ero
ero2y ago
what tutorial would that be?
Cattywampus
Cattywampus2y ago
change your character class to a regular class
[System.Serializable]
public class CharacterClass
{
public string characterClassName;
public Sprite characterClassSprite;
}
[System.Serializable]
public class CharacterClass
{
public string characterClassName;
public Sprite characterClassSprite;
}
also, scriptableobject when used as an Asset can't serialize scene objects so your Sprite there will lose the reference
Florian Voß
Florian VoßOP2y ago
Hooson
YouTube
Unity Character/Skin Selection Menu - Easy Unity Tutorial
In this unity tutorial, you'll be learning how to make a character and skin selection menu for your game. You'll learn how to use scriptable objects to make a character database. Scriptable object is a data container that you can use to save large amounts of data. Timestamps: 0:00 Intro 0:17 Character Selection UI Setup 1:32 Character Scripti...
Florian Voß
Florian VoßOP2y ago
ohhh I think that's it CharacterClass shouldnt be a SciprtableObject, you're right. Tyvm lemme see if it works now its working, thx a lot @BadaBingBadaBoom I don't really get yet when we use ScriptableObject or MonoBehavioru or none of it very new to all this
Cattywampus
Cattywampus2y ago
aight, goodluck
Florian Voß
Florian VoßOP2y ago
!close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server