Added comment and set initial assignment for the ShowEngineer and ControlBar toggle references to null.
Added comment and set initial assignment for the ShowEngineer and ControlBar toggle references to null.

// //
// Kerbal Engineer Redux // Kerbal Engineer Redux
// //
// Copyright (C) 2016 CYBUTEK // Copyright (C) 2016 CYBUTEK
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
   
namespace KerbalEngineer.Unity.Flight namespace KerbalEngineer.Unity.Flight
{ {
using KerbalEngineer.Flight; using KerbalEngineer.Flight;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
   
public class FlightEngineerMenu : CanvasGroupFader, IPointerEnterHandler, IPointerExitHandler public class FlightEngineerMenu : CanvasGroupFader, IPointerEnterHandler, IPointerExitHandler
{ {
[SerializeField] [SerializeField]
private Toggle m_ShowEngineerToggle; private Toggle m_ShowEngineerToggle = null;
   
[SerializeField] [SerializeField]
private Toggle m_ControlBarToggle; private Toggle m_ControlBarToggle = null;
   
[SerializeField] [SerializeField]
private float m_FastFadeDuration = 0.2f; private float m_FastFadeDuration = 0.2f;
   
[SerializeField] [SerializeField]
private float m_SlowFadeDuration = 1.0f; private float m_SlowFadeDuration = 1.0f;
   
/// <summary> /// <summary>
/// Gets or sets the visibility of the control bar. /// Gets or sets the visibility of the control bar.
/// </summary> /// </summary>
public bool controlBar public bool controlBar
{ {
get get
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
return DisplayStack.Instance.ShowControlBar; return DisplayStack.Instance.ShowControlBar;
} }
   
return true; return true;
} }
set set
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
DisplayStack.Instance.ShowControlBar = value; DisplayStack.Instance.ShowControlBar = value;
} }
} }
} }
   
/// <summary> /// <summary>
/// Gets or sets the visibility of the flight engineer display stack. /// Gets or sets the visibility of the flight engineer display stack.
/// </summary> /// </summary>
public bool showEngineer public bool showEngineer
{ {
get get
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
return DisplayStack.Instance.Hidden == false; return DisplayStack.Instance.Hidden == false;
} }
   
return true; return true;
} }
set set
{ {
if (DisplayStack.Instance != null) if (DisplayStack.Instance != null)
{ {
DisplayStack.Instance.Hidden = !value; DisplayStack.Instance.Hidden = !value;
} }
} }
} }
   
public void OnPointerEnter(PointerEventData eventData) public void OnPointerEnter(PointerEventData eventData)
{ {
FadeIn(); FadeIn();
} }
   
public void OnPointerExit(PointerEventData eventData) public void OnPointerExit(PointerEventData eventData)
{ {
// slow-fade out if the application launcher button is off // slow-fade out if the application launcher button is off
if (FlightAppLauncher.instance != null && FlightAppLauncher.instance.isOn == false) if (FlightAppLauncher.instance != null && FlightAppLauncher.instance.isOn == false)
{ {
FadeTo(0.0f, m_SlowFadeDuration, Destroy); FadeTo(0.0f, m_SlowFadeDuration, Destroy);
} }
} }
   
protected override void Awake() protected override void Awake()
{ {
base.Awake(); base.Awake();
   
// subscribe events // subscribe events
FlightAppLauncher.MenuClosed += MenuClosed; FlightAppLauncher.MenuClosed += MenuClosed;
FlightAppLauncher.ButtonHover += ButtonHover; FlightAppLauncher.ButtonHover += ButtonHover;
} }
   
protected virtual void OnDestroy() protected virtual void OnDestroy()
{ {
// unsubscribe events // unsubscribe events
FlightAppLauncher.MenuClosed -= MenuClosed; FlightAppLauncher.MenuClosed -= MenuClosed;
FlightAppLauncher.ButtonHover -= ButtonHover; FlightAppLauncher.ButtonHover -= ButtonHover;
} }
   
protected virtual void OnEnable() protected virtual void OnEnable()
{ {
// set starting alpha to zero and fade in // set starting alpha to zero and fade in
SetAlpha(0.0f); SetAlpha(0.0f);
FadeIn(); FadeIn();
} }
   
protected virtual void Start() protected virtual void Start()
{ {
DisplayStackToggles(); DisplayStackToggles();
} }
   
/// <summary> /// <summary>
  /// Sets a given toggle to the specified state with null checking.
  /// </summary>
  private static void SetToggle(Toggle toggle, bool state)
  {
  if (toggle != null)
  {
  toggle.isOn = state;
  }
  }
   
  /// <summary>
/// Called when the application launcher button is hovered over. /// Called when the application launcher button is hovered over.
/// </summary> /// </summary>
private void ButtonHover() private void ButtonHover()
{ {
FadeIn(); FadeIn();
} }
   
/// <summary> /// <summary>
/// Destroys the game object. /// Destroys the game object.
/// </summary> /// </summary>
private void Destroy() private void Destroy()
{ {
// disable game object first due to an issue within unity 5.2.4f1 that shows a single frame at full opaque alpha just before destruction // disable game object first due to an issue within unity 5.2.4f1 that shows a single frame at full opaque alpha just before destruction
gameObject.SetActive(false); gameObject.SetActive(false);
Destroy(gameObject); Destroy(gameObject);
} }
   
/// <summary> /// <summary>
/// Called when the display stack has loaded its settings. /// Called when the display stack has loaded its settings.
/// </summary> /// </summary>
private void DisplayStackToggles() private void DisplayStackToggles()
{ {
if (DisplayStack.Instance == null) if (DisplayStack.Instance == null)
{ {
return; return;
} }
   
SetToggle(m_ShowEngineerToggle, DisplayStack.Instance.Hidden == false); SetToggle(m_ShowEngineerToggle, DisplayStack.Instance.Hidden == false);
SetToggle(m_ControlBarToggle, DisplayStack.Instance.ShowControlBar); SetToggle(m_ControlBarToggle, DisplayStack.Instance.ShowControlBar);
} }
   
private void FadeIn() private void FadeIn()
{ {
FadeTo(1.0f, m_FastFadeDuration); FadeTo(1.0f, m_FastFadeDuration);
} }
   
private void MenuClosed() private void MenuClosed()
{ {
FadeTo(0.0f, m_FastFadeDuration, Destroy); FadeTo(0.0f, m_FastFadeDuration, Destroy);
} }
   
private void SetToggle(Toggle toggle, bool state)  
{  
if (toggle != null)  
{  
toggle.isOn = state;  
}  
}  
} }
} }