Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Stardog · Mar 15, 2014 at 02:55 PM · editornullreferenceexceptiongetcomponentaddcomponent

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);
         }
     }
 
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Mar 15, 2014 at 03:02 PM 0
Share

What does TriggerCameraSwitchScript look like?

avatar image Stardog · Mar 15, 2014 at 03:15 PM 0
Share

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.

avatar image nesis · Mar 15, 2014 at 03:20 PM 0
Share

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()?

avatar image Stardog · Mar 15, 2014 at 03:21 PM 0
Share

The problem is you can't use StartCoroutine in editor scripts. $$anonymous$$aybe it is some problem with my TriggerCameraSettings class...

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Stardog · Mar 15, 2014 at 03:38 PM 0
Share

Yes, this was the problem. One line before I had to do "tScript.settings = new TriggerCameraSettings();". Thank you!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges