Assigned variable returns null?

I have what I believe to be a very straightforward script that effectively just enabled gameobjects in the scene when certain actions are completed. I’m running into an issue where when i try to enable them they will occasionally evaluate to null and I will hit a missing reference exception as if the object has been deleted - but if i look at the object in the inspector I can see that it is definitely assigned (its a public variable.)

stuff I’ve tried:

  • I made sure there isn’t another instance of this class in the scene that hasn’t been initialized properly. there is only one instance, and I can see that its gameobjects are assigned as anticipated.

  • Reassigning the game objects at runtime. I figured maybe something was weird about how they were assigned, so I tried assigning them manually at run time in the inspector but no joy, it still gives me a missing reference exception

  • Manually enabling it before trying to reference it. I figured maybe something weird could be happening where its having trouble retrieving a disabled gameobject but that still gave me a missing reference exception.

I’ll post the code thats enabling it below as well as what it looks like in the inspector but its really about as straightforward as it gets, maybe I’m missing something very simple though.

Things that might make this weird :

  • This class and the objects its enabling are loaded into the scene via asset bundle. unity has historically had some weirdnesses associated with loading asset bundles so maybe theres a bug to do with that in play?

  • the function gets called as the result of an event being fired elsewhere. I’m not sure why this would matter since it doesnt seem like its a problem with the wrong function being called/the wrong parameter being passed but ive had troubled with events and listeners before.

any help would be appreciated.

*EDIT here is the stack trace

MissingReferenceException: The object of type ‘GameObject’ 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. HFN.Ecomuve.TrailerMap.OnPlaceTracerPin (HFN.Ecomuve.TracerModule tracerModule) (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TrailerMap.cs:32) HFN.Ecomuve.TracerTool.Update () (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TracerTool.cs:149)

using UnityEngine;
using System.Collections;
using HFN.Common;

namespace HFN.Ecomuve
{
	public class TrailerMap : MonoBehaviour 
	{
		public GameObject farmTracerPin;
		public GameObject golfTracerPin;
		public GameObject neighborhood1TracerPin;
		public GameObject neighborhood2TracerPin;
		public GameObject toiletTracerPin;


		public void PlaceTracerPin(TracerModule tracerModule)
		{
			switch(tracerModule.tracerTypes)
			{
			case TracerId.Farm :
                Debug.Log(farmTracerPin);
				farmTracerPin.SetActive(true);
				break;

			case TracerId.GolfCourse :
                Debug.Log(golfTracerPin);
				golfTracerPin.SetActive(true);
				break;

			case TracerId.Neighborhood1 :
                Debug.Log(neighborhood1TracerPin);
				neighborhood1TracerPin.SetActive(true);
				break;

			case TracerId.Neighborhood2 :
                Debug.Log(neighborhood2TracerPin);
				neighborhood2TracerPin.SetActive(true);
				break;

			case TracerId.Toilet :
                Debug.Log(toiletTracerPin);
				toiletTracerPin.SetActive(true);
				break;
			}
		}


		public void PlaceTracerPin(Tracer tracer)
		{
			switch(tracer.tracerId)
			{
			case TracerId.Farm :
				farmTracerPin.SetActive(true);
				break;

			case TracerId.GolfCourse :
				golfTracerPin.SetActive(true);
				break;

			case TracerId.Neighborhood1 :
				neighborhood1TracerPin.SetActive(true);
				break;

			case TracerId.Neighborhood2 :
				neighborhood2TracerPin.SetActive(true);
				break;

			case TracerId.Toilet :
				toiletTracerPin.SetActive(true);
				break;
			}
		}


		private void Awake()
		{
			TracerTool.onSaveTracer += PlaceTracerPin;
		}
	}
}

Well I came up with a work around but im still not sure why i was getting the behavior i was getting. If someone finds out let me know. For my workaround I basically just wrote a separate class for the objects getting enabled to track their type and their enable-able object. Then when retrieving it i check if its null and if so I iterate through the trailer map class’ children looking for it (and assign it).

Seems to work, here is the new code for that in case others find it useful.

using UnityEngine;
using System.Collections;
using HFN.Common;

namespace HFN.Ecomuve
{
	public class TrailerMap : MonoBehaviour 
	{
		public TracerPin farmTracerPin;
		public TracerPin golfTracerPin;
		public TracerPin neighborhood1TracerPin;
		public TracerPin neighborhood2TracerPin;
		public TracerPin toiletTracerPin;

        public TracerPin GetPin(TracerId id)
        {
            TracerPin pin = null;

            switch (id)
            {
                case TracerId.Farm:
                    if (farmTracerPin == null) farmTracerPin = FindPin(id);
                    pin = farmTracerPin;
                    break;

                case TracerId.GolfCourse:
                    if (golfTracerPin == null) golfTracerPin = FindPin(id);
                    pin = golfTracerPin;
                    break;

                case TracerId.Neighborhood1:
                    if (neighborhood1TracerPin == null) neighborhood1TracerPin = FindPin(id);
                    pin = neighborhood1TracerPin;
                    break;

                case TracerId.Neighborhood2:
                    if (neighborhood2TracerPin == null) neighborhood2TracerPin = FindPin(id);
                    pin = neighborhood2TracerPin;
                    break;

                case TracerId.Toilet:
                    if (toiletTracerPin == null) toiletTracerPin = FindPin(id);
                    pin = toiletTracerPin;
                    break;
            }

            return pin;
        }


        public TracerPin FindPin(TracerId id)
        {
            TracerPin pin = null;
            TracerPin[] pins = GetComponentsInChildren<TracerPin>();

            foreach (TracerPin p in pins)
            {
                if (pin.tracerId == id) pin = p;
            }

            return pin;
        }


		public void OnPlaceTracerPin(TracerModule tracerModule)
		{
            TracerPin tracerPin = GetPin(tracerModule.tracerTypes);
            Debug.Log(tracerPin);
            tracerPin.Show();
		}


		public void PlaceTracerPin(Tracer tracer)
		{
            TracerPin tracerPin = GetPin(tracer.tracerId);
            Debug.Log(tracerPin);
            tracerPin.Show();
        }


		private void Awake()
		{
			TracerTool.onSaveTracer += OnPlaceTracerPin;
		}
	}
}