- Home /
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;
}
}
}
First of all +1
for the long an detailed description. $$anonymous$$ost don't take the time to actually explain their situation ^^.
However there's still some things missing:
First, those gameobjects you reference, where are they? They don't seem to exist in the scene so they must be assets?!?
If they are assets you can't set them active you would need to instantiate them into the scene.
In your code you have two "PlaceTracerPin" methods (C%P error?). You said you load those gameobjects manually. Can we see that code?
$$anonymous$$issingComponentException seems a bit strange in this case. You don't deal with any components here.
Since you seem to receive errors, could you copy and past the exact error including the stacktrace? If you don't want to post it, have you analysed the stacktrace if there is any?
Excellent questions.
Those objects are totally in the scene, the inspector screenshot that shows the hierarchy was intended to show that there is only one trailer map object. Here is another inspector screenshot that shows where those objects are in the actual hierarchy.
I'm not sure what you mean with loading manually. the asset bundle gets loaded in when the the scene loads, i just tried manually dragging those objects into the inspector. I have tried disambiguating the PlaceTracerPin call such that I had two separate function OnPlaceTracerPin and PlaceTracerPin just in case there was some issue with the event listener but that didn't seem to make any difference.
Here is that stack trace.
$$anonymous$$issingReferenceException: 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.Trailer$$anonymous$$ap.OnPlaceTracerPin (HFN.Ecomuve.Tracer$$anonymous$$odule tracer$$anonymous$$odule) (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/Trailer$$anonymous$$ap.cs:32) HFN.Ecomuve.TracerTool.Update () (at Assets/WisdomTools/Ecomuve/UGUI/Views/Tracer/TracerTool.cs:149)
Hey,
It might help to see your TracerTool code as well. Off the top of my head, the only thing I can think of is that the variable that onSaveTracer is sending is where the actual issue is. are you sure that Tracer$$anonymous$$odule tracer$$anonymous$$odule & Tracer tracer are actually being passed in correctly?
Answer by TSI25 · Sep 14, 2016 at 08:16 PM
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;
}
}
}
Can't give you an exact answer either as it seems everything would need to be debugged as there is a lot of objects and code going on. It seems Bunny hits the answer somewhere in those points. It's bound to be one of those. I just wanted to comment that error checking is good :D
Should always check if an object is null before using it. (Good program$$anonymous$$g that helps you debug)
Your answer
Follow this Question
Related Questions
GameObject reference lost between functions 0 Answers
DontDestroyOnLoad error 2 Answers
How to use loading text 0 Answers