Impossible to build my project after this lecture

Hi everybody !!
First off all, Tank you for your course and excuse me if my english isn’t as good as i want :slight_smile:

After viewing this lecture, i can’t build my project. I’ve got this error :
Assets/Dialogue/Dialogue.cs(56,13): error CS0103: The name ‘Undo’ does not exist in the current context
And this one too :
Assets/Dialogue/Dialogue.cs(88,17): error CS0103: The name ‘AssetDatabase’ does not exist in the current context

Here is the code after the lecture :

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

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

        Dictionary<string, DialogueNode> nodeLookup = new Dictionary<string, DialogueNode>();

#if UNITY_EDITOR
        private void Awake()
        {

        }
#endif
        private void OnValidate()
        {
            nodeLookup.Clear();
            foreach (DialogueNode node in GetAllNodes())
            {
                nodeLookup[node.name] = node;
            }
        }

        public IEnumerable<DialogueNode> GetAllNodes()
        {
            return nodes;
        }

        public DialogueNode GetRootNode()
        {
            return nodes[0];
        }

        public IEnumerable<DialogueNode> GetAllChildren(DialogueNode parentNode)
        {
            List<DialogueNode> result = new List<DialogueNode>();
            foreach (string childID in parentNode.children)
            {
                if (nodeLookup.ContainsKey(childID))
                {
                    yield return nodeLookup[childID];
                }
            }
        }

        public void CreateNode(DialogueNode parent)
        {
            DialogueNode newNode = CreateInstance<DialogueNode>();
            newNode.name = Guid.NewGuid().ToString();
            Undo.RegisterCreatedObjectUndo(newNode, "Created Dialogue Node");
            if (parent != null)
            {
                parent.children.Add(newNode.name);
            }
            nodes.Add(newNode);
            OnValidate();
        }

        public void DeleteNode(DialogueNode nodeToDelete)
        {
            nodes.Remove(nodeToDelete);
            OnValidate();
            CleanDanglingChildren(nodeToDelete);
            Undo.DestroyObjectImmediate(nodeToDelete);
        }

        private void CleanDanglingChildren(DialogueNode nodeToDelete)
        {
            foreach (DialogueNode node in GetAllNodes())
            {
                node.children.Remove(nodeToDelete.name);
            }
        }

        public void OnBeforeSerialize()
        {
            if (nodes.Count == 0)
            {
                CreateNode(null);
            }

            if (AssetDatabase.GetAssetPath(this) != "")
            {
                foreach (DialogueNode node in GetAllNodes())
                {
                    if (AssetDatabase.GetAssetPath(node) == "")
                    {
                        AssetDatabase.AddObjectToAsset(node, this);
                    }
                }
            }
        }

        public void OnAfterDeserialize()
        {
        }
    }
}

Excuse me if i’m not clear but if you can get me on the good way ?
Thank you and continue doing a great job !!

Hi Oliver, welcome to the community.

It is hard to tell what is going on from the way the code is pasted in. Here is a link to how to paste code into a post so that it is formatted correctly.

https://community.gamedev.tv/t/how-to-apply-code-formatting-within-your-post/23099/2

Please repost the entire Dialogue.cs script follow the above guidelines. That will make it a lot easier to help you.

Thank you edc237. It was my first post as you can see.
I just edit it.

Thanks for cleaning up the post, much easier to read now.
The two error messages are telling you that Unity does not know what Undo and AssetDatabase are. This is because you missed a couple of things. First, there should be another using statement at the top:

using System.Collections;

Next,

This should be deleted (probably not part of the problem, but it is cluttering the code.)

Next, the block of classes for creating, deleting, and cleaning nodes all need to be inside a block like this:

#if UNITY_EDITOR
// Code for all the creating and deleting of nodes
#endif

Try these fixes and let us know if it worked or not.

One thing that may help in the future, there are links to the GitHub repo of the changes for each lecture. You can get a copy of what the instructor did and go line by line to see where you may have made a mistake.

Thank you very much for helping me.

I first tried adding the line

using System.Collections.Generic;

I get the same error messages. Then i tried putting my “CreateNode”, “DeleteNode” and “CleanDanglingChildren” classes between the “#if UNITY_EDITOR” and “#endif” lines.
That doesn’t work too…

I also tried copying and pasting the code find in the GitHub repo to see if i’m not missing something.
But apparently no.

The two error messages appear in the console only when i tried to build the game.

Maybe i should precise my version of Unity : 2022.3.22f1

You already had the above line in your script. Add:

using System.Collections;

The the instructors script has both lines. The little details are important. If your script is not working, check all the syntax and spelling. Sometimes it is hard to see the small differences. Post your script again, after the changes. Let me know if the errors are still there.

Hi edc237.

I do as you said but the errors still remains.
Then I saw that there were many changes in the script in the following lecture. So I decided to continue the course and… Victory !! The errors disappeared !!
I would have preferred to understand the error but ultimately, the result suits me :slight_smile:
You may have right on this : “The little details are important”
Thank you for your help and your time.

See you again at my next problem :slight_smile:

1 Like

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

Privacy & Terms