Array of components is broken when duplicating game object.

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.
    1195323--47398--$TestOriginal.jpg
  • 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!
    1195323--47399--$TestDuplicate.jpg
  • 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)

Shame no one else replied here, but I have this exact same issue. In fact, I was able to reproduce it in a very very simple example.

Requires: Unity 3.5.7f6
Download: HERE

Just open the main scene and duplicate the GameObject called ‘Duplicate Me’ a bunch of times. You’ll notice that 50% of the time you’ll get an error message.

The error message gets thrown when I detect that a component wasn’t correctly duplicated. I tested this code on Windows and Mac and was able to reproduce it 100% of the time using Unity 3.5.7f6 (non-pro).

This is a very serious bug, and I’m surprised more people haven’t noticed it (it took me long enough). I’ve already submitted a bug report, but I don’t really see it getting fixed anytime soon, and my current project kind of relies on GameObject duplication working.

Yes, I feel your pain. I’m fortunately able to work with it for now, but it is a serious problem. I imagine not too many others have encountered it since it’s somewhat obscure. Thanks for submitting the report. Is there a way I can upvote that report?