I’m stumped on this one, so help would be much appreciated! Either I’m doing something wrong, or I found a nefarious bug in Unity.
Please see the attached project. Built with Unity Pro 3.5.7f6. This has been greatly simplified from a very complex set of scripts.
Synopsis
When duplicating an object with components that have lists or arrays of references to other components of the same type, the array references get hosed, but they are not exactly null. It’s the same whether using a built-in array or Generic.List. Fiddling with attributes can cause the arrays to magically right themselves. Or saving and reopening the scene will repair the damaged internal pointers. But I can’t figure out a way to fix this so that the duplicate behaves properly right away.
Steps to Reproduce
- Open the TestScene and look at the TestObject in the inspector. There are 2 instances of the Test component. The first has an empty array while the second has 1 reference to the first Test component. If you click the ‘LIST REFERENCES’ button on the 2nd component, it will list each of the references recursively in the Console and everything looks good.

- Now duplicate the TestObject, and repeat the test by clicking the button in the inspector for the duplicate, again on the second component. This time however, the console output has an odd empty reference and a null reference error for the array, which should not at all be null! I’ve even put in the instance ID of the object, so it would appear that the pointer is correct. If you click the button on the first component, you’ll see that its array is not null and it reports fine. Something very screwy is going on here!

- Here’s the extra weird part: if you now alter the Name field of each component, then try the test again, it works! It’s as though Unity’s internal memory is working with a stale temporary duplicate, then changing something forces it to update the references. Just speculating here. Also, if you save and reopen the scene, it works fine.
I should point out also, in case you feel this test is too convoluted, is that the array works fine when you’re dealing with a direct reference, but it’s broken when you’re dealing with a reference through the array, thus the need for 2 instances of the Test component. Think of it this way:
- TestA.References = GOOD
- TestB.References[0].References = BAD (where References[0] points to TestA)
All source code is included in the project, but here it is just in case:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode()]
public class Test : MonoBehaviour {
public string Name = "";
public Test[] References = null;
void Awake () {
//Debug.Log("Awake");
if(References == null) {
References = new Test[1];
}
}
public void DebugReferences () {
if(References != null) {
int i = 0;
foreach(Test t in References) {
if(t != null) {
Debug.Log("References["+i+"] = "+t.Name+" :"+t.GetInstanceID());
t.DebugReferences();
}
else {
Debug.Log("References["+i+"] = Empty");
}
i++;
}
}
else {
Debug.LogError("References IS NULL");
}
}
}
1195323–47400–$TestProject.zip (256 KB)