C
C#16mo ago
Florian Voß

❔ ✅ 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
ero16mo ago
try putting the attribute on the fields directly
Florian Voß
Florian Voß16mo 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
ero16mo ago
[SerializeField]?
Florian Voß
Florian Voß16mo ago
oh
Florian Voß
Florian Voß16mo ago
same result
Florian Voß
Florian Voß16mo ago
@Ero
ero
ero16mo ago
no clue
Florian Voß
Florian Voß16mo 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
ero16mo ago
what tutorial would that be?
Cattywampus
Cattywampus16mo 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ß16mo 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ß16mo 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
Cattywampus16mo ago
aight, goodluck
Florian Voß
Florian Voß16mo ago
!close
Accord
Accord16mo 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
More Posts