DontDestroyOnLoad destroys public variables

using UnityEngine;
using System.Collections;

public class dontDes : MonoBehaviour {

    // Use this for initialization

    public static dontDes ds; //reference variable
   
    void Awake ()
    {//awake happens before start
        //if there is no current game control make this the game control
        if (ds == null) { //if this variable has not been assigned
            DontDestroyOnLoad (gameObject);//dont destroy the object
            ds = this;
           
        } else if (ds != this) {//control does exist
            //if this is not control, then destory it
            Destroy (gameObject);
        }
    }


}

this on Attached to my HUDCanvas.

From the first scene, MainMenu, players can navigate to the second scene, the first level.

Here the HUD component gets declared and instantiated. Inside a script, I have public variables and they use the HUDCanvas childrens as components. (drag and drop).

The problem is, when I go back to the main menu, from the first level. And then back to the first level from the main menu, in the inspector where I dragged the HUDCanvas’s childrens (componenets) it says

MissingReferenceException: The object of type 'Image' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.

My question is, I don’t destroy my CanvasHUD, so why do the public variables get destroyed.

It’s best to have an init scene and declare DontDestroyOnLoad in the awake and go to the menu or scene you are working on in the start.

You have to check

else if (ds != null) {//control does exist
            //if this is not control, then destory it
            Destroy (gameObject);
        }

because the new Awake() is called in the new instance of GameObject