When is Node object created?

I don’t understand when a node object is created. To debug node.coordinates and node.isWalkable in the console, we need to have an instance of the Node class somewhere. What is strange to me is that we never used the new keyword, like when Gary showed us with Vector2Int as an example, but the message was still printed on the console. I tested this for a while and noticed that we will get an error (NullReferenceException) until we put [System.Serializable] at the top of the Node class. Can someone explain it to me? I have already watched this lecture twice and it is still incomprehensible to me.

Also, I’m not sure why we’re using pure C# instead of Monobehavior. When to use pure C# classes, and when not?

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.

4 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms