- Home /
AddComponent giving NullReferenceException in Editor script
Having an issue using AddComponent to add a script to a newly created prefab. This is in C#. I have the script "scriptType1.js" located in my 'Assets\Scripts\' folder.
The errors I'm getting are as follow, but I have no idea what it all means. My script can't get past "AddType1Stuff()" function.
NullReferenceException: Object reference not set to an instance of an object CreatePrefabFromModels.AddType1Stuff (UnityEngine.GameObject newPrefab) (at Assets/Editor/CreatePrefabFromModels.cs:142) CreatePrefabFromModels.createNew (UnityEngine.GameObject obj, System.String localPath) (at Assets/Editor/CreatePrefabFromModels.cs:118) CreatePrefabFromModels.CreatePrefab (UnityEngine.GameObject prefab) (at Assets/Editor/CreatePrefabFromModels.cs:96) CreatePrefabFromModels.OnWizardCreate () (at Assets/Editor/CreatePrefabFromModels.cs:65) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) UnityEditor.ScriptableWizard.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/MonoGenerated/Editor/ScriptableWizard.cs:70) System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/GUI/DockArea.cs:213) UnityEditor.HostView.Invoke (System.String methodName) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/GUI/DockArea.cs:206) UnityEditor.HostView.OnGUI () (at C:/BuildAgent/work/6bc5f79e0a4296d6/Editor/Mono/GUI/DockArea.cs:107)
CODE:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class CreatePrefabFromModels : ScriptableWizard
{
public GameObject[] prefabsToCreate;
[MenuItem ("Custom/Create Prefab from 3D Models")]
static void CreateWizard ()
{
ScriptableWizard.DisplayWizard<CreatePrefabFromModels>("Create Prefab from 3D Models", "Apply");
}
void OnWizardCreate ()
{
foreach (GameObject prefabToCreate in prefabsToCreate)
{
CreatePrefab(prefabToCreate);
}
}
static void CreatePrefab (GameObject prefab)
{
string localPath = "Assets/Prefabs/prefab_" + prefab.name.ToLower() + ".prefab";
if (AssetDatabase.LoadAssetAtPath(localPath, typeof(GameObject)) )
{
if (EditorUtility.DisplayDialog("Are you sure?", "The prefab already exists. Do you want to overwrite it?", "Yes", "No"))
{
createNew(prefab, localPath);
}
else
Debug.Log("File was not overwritten");
}
else
{
createNew(prefab, localPath);
}
}
static void createNew(GameObject obj, string localPath)
{
Object newPrefab = EditorUtility.CreateEmptyPrefab(localPath); // create new empty prefab
EditorUtility.ReplacePrefab(obj, newPrefab); // put the 3d model into the new prefab
if (obj.name.ToLower().Contains("type1"))
{
AddType1Stuff(newPrefab as GameObject); // have to say 'as GameObject' since AddComponent won't work with an 'Object'
}
else if (obj.name.ToLower().Contains("type2"))
{
AddType2Stuff(newPrefab as GameObject);
}
else
{
Debug.Log("Object is not Type1 or Type2");
}
AssetDatabase.Refresh();
GameObject clone = EditorUtility.InstantiatePrefab(newPrefab) as GameObject;
}
static void AddType1Stuff(GameObject newPrefab)
{
newPrefab.AddComponent<scriptType1>();
newPrefab.AddComponent<MeshCollider>();
}
static void AddType2Stuff(GameObject newPrefab)
{
newPrefab.AddComponent<scriptType2>();
}
}
Answer by Bunny83 · Jun 15, 2011 at 01:08 AM
If you use ReplacePrefab you should call AssetDatabase.Refresh(); and reload the prefab because the AssetDatabase isn't up to date.
Also it would make more sense to build your GameObject and then replace the prefab and not the other way round.
if (obj.name.ToLower().Contains("type1"))
{
AddType1Stuff(obj);
}
else if (obj.name.ToLower().Contains("type2"))
{
AddType2Stuff(obj);
}
else
{
Debug.Log("Object is not Type1 or Type2");
}
Object newPrefab = EditorUtility.CreateEmptyPrefab(localPath);
EditorUtility.ReplacePrefab(obj, newPrefab);
AssetDatabase.Refresh();
newPrefab = AssetDatabase.LoadAssetAtPath(localPath,typeof(GameObject));
GameObject clone = EditorUtility.InstantiatePrefab(newPrefab) as GameObject;
By the way, i don't recommend to use the "as" cast since it will return null when it can't be casted. The "normal" c-style cast will throw an Invalid-Cast-Exception if it can't be casted.
Thanks, I'll try it out! Didn't realize I could add components like scripts directly to the 3d $$anonymous$$odel file, since it wouldn't let me drag and drop onto it from the inspector.
What is the other way to cast? I'm new to C#, and had seen 'as' somewhere so I used that.
Your answer
Follow this Question
Related Questions
NullReferenceException - Editor script using GetComponent after AddComponent 1 Answer
applying toon shading to all 3d meshes at once 1 Answer
Editor Window/Scriptable object - null problem 2 Answers
GetComponent and AddComponent with variables for Collider type 2 Answers
ReorderableList Custom Editor Count -> NullReferenceExeception 1 Answer