- Home /
NullReferenceException - Editor script using GetComponent after AddComponent
I get a NullReferenceException in this editor script when trying to set the value of a component right after adding it. Normally I could wait a frame, but how do I do that in the editor?
using UnityEngine;
using System.Collections;
using UnityEditor;
public class MyTools : ScriptableWizard
{
private GameObject camGO;
private GameObject triggerGO;
[MenuItem("Tools/My/Create Camera and Trigger at Position")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard<MyTools>("Create Camera & Trigger");
}
void OnWizardCreate()
{
// CREATE CAMERA GO
camGO = new GameObject("Camera");
camGO.AddComponent<Camera>();
// CREATE TRIGGER GO
triggerGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
triggerGO.name = "Trigger";
// ADD COMPONENT
TriggerCameraSwitchScript tScript = triggerGO.AddComponent<TriggerCameraSwitchScript>();
// SET COMPONENT VALUES
tScript.settings.cameraToSwitchTo = camGO; // NULL REFERENCE ERROR
// POSITION
camGO.transform.position = SceneView.lastActiveSceneView.camera.transform.position;
camGO.transform.rotation = SceneView.lastActiveSceneView.camera.transform.rotation;
}
}
TriggerCameraSwitch.cs
public class TriggerCameraSwitchScript : MonoBehaviour {
public TriggerCameraSettings settings;
public bool bEnableRenderer;
#region Events
public delegate void EventHandler(TriggerCameraSettings triggerCameraSettings);
public static event EventHandler CameraSwitchTriggerEntered;
#endregion
void Start()
{
if (bEnableRenderer)
{
renderer.enabled = true;
}
else
{
renderer.enabled = false;
}
}
void OnTriggerEnter(Collider collider)
{
if (collider.tag != "PlayerColliderCameraSwitch") return;
// SEND EVENT
if (CameraSwitchTriggerEntered != null)
CameraSwitchTriggerEntered(settings);
}
void OnDrawGizmosSelected()
{
if (settings.cameraToSwitchTo != null)
{
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, settings.cameraToSwitchTo.transform.position);
}
}
}
I updated it. The TriggerCameraSettings class itself has mostly just "public GameObject cameraToSwitchTo;" and the constructor. It is System.Serializable if that matters.
I tried AddComponent then GetComponent on another script and it works fine, just not when I'm using a custom class.
I won't pretend to be sure this is the right way to do it, but could you start a coroutine that yields with WaitForEndOfFrame(), then uses GetComponent()?
The problem is you can't use StartCoroutine in editor scripts. $$anonymous$$aybe it is some problem with my TriggerCameraSettings class...
Answer by whydoidoit · Mar 15, 2014 at 03:22 PM
The TriggerCameraSettings are not being initialized when you use AddComponent. They are being initialized the next time the serializer sees your object (after this code exits). You should create a default instance of the settings in the Awake of the script (or by a default initializer) to ensure that they are available or create the settings directly in your editor script.
Yes, this was the problem. One line before I had to do "tScript.settings = new TriggerCameraSettings();". Thank you!
Your answer
Follow this Question
Related Questions
Getting component variables in InitializeOnLoad editor script? 1 Answer
AddComponent giving NullReferenceException in Editor script 1 Answer
NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer
GetComponent vs AddComponent 3 Answers
Console throwing errors when trying to rename or highlight elements. 1 Answer