Missing Reference after null Check

On start I try to run this, but get
“MissingReferenceException: The object of type ‘Transform’ 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.”

    if(thing!=null){//thing is referenced in the editor, so i know its there
          print(thing.name.ToString());//I can access the name value of the object
          //^that prints its name
          print(thing.position.ToString());//error here
          if(thing.childCount>0){//also error here saying it got destroyed or is missing
          //it will only let me access its name

Whats going on? T.T

Edit: Can parts of objects die? I can only access the name value so far. I get the missing Reference error when i try to access this thing’s position or child count.

Most likely that fixed worked because you are changing MenuState.trans in another script or another place. foreach doesn’t work if you modifiy whatever list it’s looping through. So since you stored a local copy, that wasn’t changing.

I had this same problem. I had a List of some transforms in the hierarchy. Then in a custom inspector, I have that script iterate over that list looking for any null references which might indicate that the transform has been removed from the hierarchy, like so:

if (sceneItems _== null || sceneItems*.parent != transform)*_

* sceneItems.RemoveAt(i);*

But to my frustration, the transform references pass the null check but throw a MissingReferenceException when I dereference the transform’s parent! Even if I separate it out as an “else” case, it still passes the null check but then throws an exception when I check the .parent member. As in your case, I was able to safely dereference the .name property, but not the .parent property. That doesn’t make sense to me at all. As you said, it seems like the component is only “partially dead” - with certain members still safely accessible, but others throwing an exception.
This isn’t really an answer, but I finally just had to nest the code in question in a try/catch block. For my purposes, it’s okay because it should only ever encounter a destroyed transform while in-editor. But were this code that had to run on a mobile device, this approach would mean you couldn’t use the “Fast but no exceptions” script call optimization setting in the player build settings.

Wish I had an answer, but wanted to chime in that as of Unity 4.5.4 this is still an issue. Confirmed on several unity installations on several operating systems. It seems that any time Transforms and Arrays get together in a non-trivial fashion the deserialized Transforms are corrupted in some fashion.

I have a MonoBehaviour with an array of structs, each struct containing a Transform reference and a float value. When they deserialize all the data is valid (looking in the Editor you can see all the Transforms are linked correctly and clicking on one will property select it). Once any C# code attempts to access any properties on the Transform though,

MissingReferenceException: The object of type ‘Transform’ 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.

The code throwing the exception is as follows

f( layer != null ) {
	m_startingLocalPosition = layer.localPosition;

In case it’s not clear… I am NEVER destroying layer and it is already inside a null check (as you can see). So thanks for the patronizing error message.

If I serialize “layer” as a GameObject and grab the GO’s transform property everything works fine, except I don’t to have the interface I want in the editor and am forced to make a bunch of unnecessary (and perplexingly expensive) calls to GameObject.get_Transform.

So this problem has existed for over a year and hasn’t been fixed… as far as I can tell it hasn’t even been acknowledged by any Unity employees. Geez. Here’s a tip Unity, bugs like this… handled this poorly… Well, if you wanted Epic to have my money that badly you could have just asked instead of wasting so much of my time.

I encountered this error myself in Unity 5.2 2, and was able to diagnose and repair the problem.

I had a prefab with a Soldier. The original version of the code had a variable weapon.

 public class Soldier : MonoBehavior {
   public GameObject weapon;        
   public void Attack()
     if (weapon != null) {
        Vector3 pos == weapon.transform.position;

However, this was later refactored to change the type of weapon.

public class Soldier : MonoBehavior {
   public Transform weapon;
   public void Attack()
       if (weapon != null) {
           // MissingReferenceException in Editor
           // Crash on device.
           Vector3 pos == weapon.position;

Turns out the prefab was still pointing to the fileID of the original weapon GameObject , and not its transform. I was still able to see the variable assigned in the Inspector, and even click-follow it to the right place. However, the icon was a blue square signifying a GameObject.

To resolve the issue, I manually reassigned the variable by dragging the same exact GameObject in the inspector, and observed the icon change to the 3-line transform icon (with the same name).

I suspect might have gotten into this state due to a merge conflict or some other file related error, but I’m not sure.