3.1.0f4: Default values ignored when class is in an array

Can I get some folks to test one or two scripts for me? I believe there’s a bug involved, but I’d like to eliminate my system or setup from the equation… I’ve got two test scripts that do the same thing, one in CS and the other in JS…

CS: http://littleangel.pastebin.com/HyWqXdR5 JS: http://littleangel.pastebin.com/Q1TuJZbY

Here’s the code in-line:

using UnityEngine;
using System.Collections;


[System.Serializable]
public class TestClass {
	public Color defaultColor = Color.white;
	public Vector3 defaultOffset = new Vector3 (1.0f, -1.0f, 2.0f);
	public int defaultInt = 5;
	public float defaultFloat = 12.345f;
	public bool defaultBoolTrue = true;
	public string defaultString = "Test String";
}

public class ArrayTestCS : MonoBehaviour {
	public TestClass[] testClassArray = null;
}
class TestClass {
	var defaultColor : Color  = Color.white;
	var defaultOffset : Vector3  = new Vector3 (1.0f, -1.0f, 2.0f);
	var defaultInt : int  = 5;
	var defaultFloat : float  = 12.345f;
	var defaultBoolTrue : boolean  = true;
	var defaultStrint : String = "Test String";
}


var testClassArray : TestClass[]  = null;

And if someone could load those, pop them onto a new empty GO and then make the array size in the inspector 1 or 2 and see if any of the default values from the original class populate any of the instances in the array?

For me all of the set defaults are coming up 0.0, null, Color.black, false, Vector3.zero… no matter what I put in the declaration of the variables in the original class.

This is what I’d expect to get:

This is what I get:

This was working in 3.0, so it must have been introduced in 3.1 - unless it is something related to my installation - which is what I’m trying to rule out.

Oddly, if I edit the class AFTER the script has been attached to a GameObject, the default values appear.

Here, after the script has been attached, I’ve duplicated and renamed two lines, defaultColor and defaultOffset:

using UnityEngine;
using System.Collections;


[System.Serializable]
public class TestClass {
	public Color defaultColor = Color.white;
	public Color defaultColor2 = Color.white;
	public Vector3 defaultOffset = new Vector3 (1.0f, -1.0f, 2.0f);
	public Vector3 defaultOffset2 = new Vector3 (1.0f, -1.0f, 2.0f);
	public int defaultInt = 5;
	public float defaultFloat = 12.345f;
	public bool defaultBoolTrue = true;
	public string defaultString = "Test String";
}

public class ArrayTestCS : MonoBehaviour {
	public TestClass[] testClassArray = null;
}

… the I’ve saved and compiled:

… and the default values show up.

but if I do anything that resets or recreates the array, like using the reset option:

… or setting the array to 0

When I recreate/repopulate the array, it comes up set to zeros again…

I have nothing to add other than that I’m pretty sure I’ve observed this behavior before as well. I haven’t tried the code you posted, but I think I’ve seen the same behavior when creating arrays whose type is a custom struct or class. (I don’t remember it working prior to 3.1 though - I was under the impression it’s always been this way.)

This is a shot from an earlier saved version of my project:

I know for a fact that I never set Default Color or Drop Shadow Offset, or for that matter the Drop Shadow check box. The only thing I can think that has happened between then and now is 3.1.0f4… Could this have been 2.6 behaviour that was fixed in 3.0, but has now re-broken?

That seems possible (I think I was using 2.x when I noticed this behavior). I’d have to check to be sure though…

Humph… An I can’t set these values in Awake. This is an add-on for distribution, so I need defaults set in the inspector for the end-user to override if necessary.

Bargle.

Interesting… This seems to have something to do with initialization. Instead of just declaring the array, like this:

public class ArrayTestCS : MonoBehaviour {
	public TestClass[] testClassArray;
}

or this:

public class ArrayTestCS : MonoBehaviour {
	public TestClass[] testClassArray = null;
}

and I modify the code like this and give the array an initial size:

public class ArrayTestCS : MonoBehaviour {
	public TestClass[] testClassArray = new TestClass[1];
}

… then the defaults come through.

2 Likes