Hello there!
Yes, to access the node’s data you need an instance of that class somewhere, and that’s pretty much what marking a variable as [SerializeField]
does, as you see in the inspector, it automatically creates a class with empty data, that’s why you don’t need to use the keyword new
to create a new instance.
If you remove the [System.Serializable]
attribute the class isn’t serializable anymore, meaning that… well it means a lot of things, I rather put the link of what it does: Unity - Manual: Script serialization For practicle purposes, you can’t mark it as [SerializeField]
, you won’t be able to access it in the inspector anymore, meaning no class is being automatically created.
When to use pure C# classes? That’s a very good question with a very tricky answer, because it depends, you can use them pretty much anywhere all the time except for those scripts that need to be attached to a game object, for instance:
class StatsUpdater
{
[SerializeField] int intelligence;
[SerializeField] int agility;
[SerializeField] int strength;
You can have a class with that sort of system, where the stats are predetermined in the script, but lets say you need your script to able to pick from any stats that are provided, you could grab all those variables and put them in a separate generic class.
class Stats
{
[SerializeField] int intelligence;
[SerializeField] int agility;
[SerializeField] int strength;
class StatsUpdater
{
[SerializeField] Stats startingStats;
You could do that, or even something like this:
class StatsUpdater
{
public void UpdateStats(Stats statsToUpdate)
{
//Do things
As you can see, generic classes give far more flexibility, but you don’t necessarily need them for every project, so it all comes down to your experience as a coder.
You can also create your own type with them, as Gary pointed out, a Vector is a class that we use as a variable, you could use that approach to even create your very own libraries.
I highly recommend checking out AI systems like Behavior Trees or GOAP, those systems use generic classes a lot, and I mean a lot, so you can have a little more practice.
Hope this helps you to better understand generic C# classes.