Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Tutorial Error: Creator Kit - Beginner Code

Discussion in 'Scripting' started by elijahko, May 26, 2020.

  1. elijahko

    elijahko

    Joined:
    Mar 26, 2018
    Posts:
    3
    Hi Unity,

    When I am following your "Creator Kit - Beginner Code" tutorial at https://learn.unity.com/tutorial/customize-the-health-potions. In step 2, I have encountered the following problem:

    When I follow Beginner Code > Create Item Effect > Enter a name without spaces (e.g., “AddHealthEffect”) and select Create. I can't create the name, there is no response, instead, I get the following error:

    NullReferenceException: Object reference not set to an instance of an object
    NameWindow.OnGUI () (at Assets/Creator Kit - Beginner Code/Scripts/Editor/CreateScriptEditor.cs:178)

    I am using Unity 2019.3.14f1.
    It seems some other users also encounter the same problem.

    Many Thanks,
    Elijah
     
  2. elijahko

    elijahko

    Joined:
    Mar 26, 2018
    Posts:
    3
    Here is the code of "CreateScriptEditor.cs"

    using System.IO;
    using UnityEngine;
    using UnityEditor;
    using System.CodeDom.Compiler;

    public class CreateScriptEditor
    {
    [MenuItem("Beginner Code/Create Item Effect")]
    static void CreateItemEffect()
    {
    var win = EditorWindow.GetWindow<NameWindow>();
    win.Display();

    win.OnValidate = s =>
    {
    string[] asset = AssetDatabase.FindAssets("SampleItemEffect");

    if (asset.Length > 0)
    {
    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(AssetDatabase.GUIDToAssetPath(asset[0]));

    string result = textAsset.text.Replace("{EFFECTNAME}", s);

    string targetPath = Application.dataPath + "/Scripts/ItemEffect/";
    Directory.CreateDirectory(targetPath);

    targetPath += s + ".cs";
    File.WriteAllText(targetPath, result);
    AssetDatabase.Refresh();

    Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(targetPath.Replace(Application.dataPath, "Assets"));
    EditorWindow.GetWindow(System.Type.GetType("UnityEditor.ProjectBrowser, UnityEditor"));
    EditorGUIUtility.PingObject(Selection.activeObject);
    }
    else
    {
    Debug.LogError("Couldn't find the sample item effect script file");
    }
    };
    }

    [MenuItem("Beginner Code/Create Weapon Attack Effect")]
    static void CreateWeaponEffect()
    {
    var win = EditorWindow.GetWindow<NameWindow>();
    win.Display();

    win.OnValidate = s =>
    {
    string[] asset = AssetDatabase.FindAssets("SampleWeaponEffect");

    if (asset.Length > 0)
    {
    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(AssetDatabase.GUIDToAssetPath(asset[0]));

    string result = textAsset.text.Replace("{EFFECTNAME}", s);

    string targetPath = Application.dataPath + "/Scripts/WeaponEffect/";
    Directory.CreateDirectory(targetPath);

    targetPath += s + ".cs";
    File.WriteAllText(targetPath, result);
    AssetDatabase.Refresh();

    Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(targetPath.Replace(Application.dataPath, "Assets"));
    EditorWindow.GetWindow(System.Type.GetType("UnityEditor.ProjectBrowser, UnityEditor"));
    EditorGUIUtility.PingObject(Selection.activeObject);
    }
    else
    {
    Debug.LogError("Couldn't find the sample weapon effect script file");
    }
    };
    }

    [MenuItem("Beginner Code/Create Equipped Effect")]
    static void CreateEquippedEffect()
    {
    var win = EditorWindow.GetWindow<NameWindow>();
    win.Display();

    win.OnValidate = s =>
    {
    string[] asset = AssetDatabase.FindAssets("SampleEquipmentEffect");

    if (asset.Length > 0)
    {
    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(AssetDatabase.GUIDToAssetPath(asset[0]));

    string result = textAsset.text.Replace("{EFFECTNAME}", s);

    string targetPath = Application.dataPath + "/Scripts/EquippedEffect/";
    Directory.CreateDirectory(targetPath);

    targetPath += s + ".cs";
    File.WriteAllText(targetPath, result);
    AssetDatabase.Refresh();

    Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(targetPath.Replace(Application.dataPath, "Assets"));
    EditorWindow.GetWindow(System.Type.GetType("UnityEditor.ProjectBrowser, UnityEditor"));
    EditorGUIUtility.PingObject(Selection.activeObject);
    }
    else
    {
    Debug.LogError("Couldn't find the sample equipped effect script file");
    }
    };
    }

    [MenuItem("Beginner Code/Create Interactable Object")]
    static void CreateInteractableObject()
    {
    var win = EditorWindow.GetWindow<NameWindow>();
    win.Display();

    win.OnValidate = s =>
    {
    string[] asset = AssetDatabase.FindAssets("SampleInteractableObject");

    if (asset.Length > 0)
    {
    var textAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(AssetDatabase.GUIDToAssetPath(asset[0]));

    string result = textAsset.text.Replace("{EFFECTNAME}", s);

    string targetPath = Application.dataPath + "/Scripts/InteractableObjects/";
    Directory.CreateDirectory(targetPath);

    targetPath += s + ".cs";
    File.WriteAllText(targetPath, result);
    AssetDatabase.Refresh();

    Selection.activeObject = AssetDatabase.LoadMainAssetAtPath(targetPath.Replace(Application.dataPath, "Assets"));
    EditorWindow.GetWindow(System.Type.GetType("UnityEditor.ProjectBrowser, UnityEditor"));
    EditorGUIUtility.PingObject(Selection.activeObject);
    }
    else
    {
    Debug.LogError("Couldn't find the sample interactable script file");
    }
    };
    }
    }

    public class NameWindow : EditorWindow
    {
    public System.Action<string> OnValidate;

    string m_EffectName;

    CodeDomProvider _provider;

    public void Display()
    {
    var pos = position;
    pos.size = new Vector2(400, 300);
    position = pos;

    m_EffectName = "";

    if (_provider == null)
    _provider = CodeDomProvider.CreateProvider("CSharp");

    ShowModalUtility();
    }

    void OnGUI()
    {
    m_EffectName = EditorGUILayout.TextField("Effect Name", m_EffectName);

    bool validName = _provider.IsValidIdentifier(m_EffectName);

    EditorGUILayout.BeginHorizontal();

    GUI.enabled = validName;
    if (GUILayout.Button(validName ? "Create" : "Invalid Name"))
    {
    OnValidate(m_EffectName);
    Close();
    }

    GUI.enabled = true;
    if (GUILayout.Button("Cancel"))
    {
    Close();
    }

    EditorGUILayout.EndHorizontal();

    if (!validName)
    {
    EditorGUILayout.HelpBox("The name is not valid. It shouldn't contains space, start with a number or contains special character like ; or .", MessageType.Error);
    }
    }
    }
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
  4. chamotion

    chamotion

    Joined:
    May 29, 2020
    Posts:
    3
    I had the same problem. I had to move the win.Display() after the win.OnValidate definition in CreateScriptEditor.cs. I did this for each of the Create* menu items since they probably all had the same issue.

    Code (CSharp):
    1.     static void CreateItemEffect()
    2.     {
    3.         var win = EditorWindow.GetWindow<NameWindow>();
    4.  
    5.         win.OnValidate = s =>
    6.         {
    7.             ...
    8.         }
    9.  
    10.         win.Display();
    11.     }
     
    Codie_Cal and uximer like this.
  5. DJVarma

    DJVarma

    Joined:
    Aug 19, 2020
    Posts:
    4
    While I was following the tutorial, I suddenly realised that 21 errors appeared. All of these errors had something to do with different scripts. I tried everything I know of, including undoing what I did and restoring. I will post the code here. Can someone please help me?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    It's always best to start a fresh post rather than hijacking some random half-a-year-old post.

    Make your own post please, it's free. And when you do, since none of us are mind readers and cannot imagine what your 21 errors are, follow this simple guideline:

    Please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
  7. DJVarma

    DJVarma

    Joined:
    Aug 19, 2020
    Posts:
    4
    sorry, I will do so now