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;
}
}
}