Null Reference, dialogue object won't create a default node?

I made sure to copy the code exactly as per the video, but I keep getting null reference errors coming from the dialogue script:
image

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace RPG.Dialogue
{
    [CreateAssetMenu(fileName = "New Dialogue", menuName = "Dialogue", order = 0)]
    public class Dialogue : ScriptableObject
    {
        [SerializeField]
        List<DialogueNode> nodes;

#if UNITY_EDITOR
        private void Awake()
        {
            if (nodes.Count == 0) //error is referencing this line
            {
                nodes.Add(new DialogueNode());
            }
        }
#endif
        public IEnumerable<DialogueNode> GetAllNodes()
        {
            return nodes;
        }
    }
}

image

I can’t quite pinpoint the reason why it won’t create a default node?

Just to confirm, I am using version 2020.1.3f1 as advised in the project set up video, however this doesn’t appear to be a versioning issue.

As an aside, upgrading the RPG project to 2020 broke all of the animations and combat logic, is this version absolutely mandatory for this course?

Update: The animation issue is resolved. Must have just been a glitch, because opening the Animator window and closing it again cleared it right up. Still not sure whats wrong with my dialogue nodes though!

size of array is zero. Thats reason. Type any other number.

This line is your culprit. The list doesn’t technically exist in Awake() the first time you create a Dialogue because it’s not initialized in the field declaration.
Try this:

[SerializeField] List<DialogueNode> nodes = new List<DialogueNode>();
2 Likes

Thank you Brian, that fixed the issue!

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

Privacy & Terms