Strange behaviour

Hello all :slight_smile:

I had a strange behaviour in my C# script…

Consider this:

public class blabla : MonoBehaviour
{

public List my_list = new List();

// the constructor
public blabla()
{
my_list.Add(new Vector3(0f,1f,2f);
my_list.Add(new Vector3(0f,1f,2f);
my_list.Add(new Vector3(0f,1f,2f);
…etc…
}

etc…

I stick this script on my GameObject, and modify the Vectors3s
in my list in editor, and… when i run the project all is all good :slight_smile:
The Vectors i set are kept as i set them…
This is strange, as the constructor is called at startup, but the
existing list isn’t changed at all… well. I thought Unity had it’s
own builtin constructor call method, and i thought it was cool as i
could keep my ‘3D visual modifications’ i did in the editor without
setting them up in code in a hardcoded way…
OK

Now, i decide to replace my Vector3s by some struct of my own…

like:

public struct my_struct{
Vector3 blah1;
float blah2
int etc.
}

my list becoming:
public List<my_struct> my_list = new List<my_struct>();

and my constructor becoming:

// the constructor
public blabla()
{
my_struct my_data;
my_data.blah1 = new Vector3(0f,0f,0f);
my_data.blah2 = 0.1f;
my_data.etc = 10;
my_list.Add(my_data);
…

Damned !!!
this time, when constructor is called ( i mean at project open, object selection,
clic on play button, etc… my list is simply reset at the constructor default value,
and my class looses all the changes i made on my list elements !!!

Does anyone have an idea of what i do wrong ? Is there some special handling
on unity3D’s native types constructors, that don’t exist on other types ?

Please help !

Ofc, i could handle several lists, one list for each type, but this would really be
crappy coding and slow runtime…

Thanks to all for your precious help :slight_smile:
Have a nice day

Stay far, far away from the MonoBehaviour constructor. It’s completely unsupported and gets invoked on all sorts of strange points. In stead use Awake/OnEnable/Start.

For your use, however it sounds like what you want to do is set up some default values for your behaviour. I’d recommend you use the Reset method for that in stead:

void Reset ()
{
    my_list.Add(new Vector3(0f,1f,2f);
    my_list.Add(new Vector3(0f,1f,2f);
    my_list.Add(new Vector3(0f,1f,2f);
}

Wow ! so it seems i got to ban the constructor from my code :cry:

Thanks for the tip.
However, what i need, is just the same behavior as i have with my Vector3 list.
I mean then i do
list.Add(new vector3… );
in the constructor, everything is ok, and the point i change in unity editor are kept
as i changed them, event when i clic on ‘play’ button…

My problem is when i replace the vector3s in my list with my own struct…
The list elements are squished by my constructor. and i don’t want this…
However, i guess there are some hidden undocumented thingies i stepped in :frowning:
I think i gonna go the crappy way… several lists of native unity classes…
really deceiving btw :frowning:
have a nice day :slight_smile: and thanks for your answer

I’m not fully following what it is you need, but try using Reset as described above and add the [System.Serializable] attribute to your custom class.

OMFG !!!

ok ok :slight_smile: i got it and then saw your post… you found the trick !
What i did, is externalizing my struct to a class…

public class my_struct{
Vector3 blah1;
float blah2
int etc.
}

modified the point in the editor and… pressing ‘PLAY’ reset the list at it’s default values set in constructor…
I tried your Reset way… seemed to work fine, but… my list simply emptied at ‘PLAY’
throwed avay the ‘Reset’ way and i found a thread talking about lists of custom structs:
http://forum.unity3d.com/threads/78471-C-how-can-I-create-and-access-from-the-GUI-an-array-or-list-of-custom-struct

then found the unity’doc page:

… implicitely telling me i’m too stoopid to RTFM :stuck_out_tongue_winking_eye:

just adding [System.Serializable]
before my ‘structure’ class did solve my problem !!
Now it works pretty well :slight_smile: and my list don’t get swept at ‘PLAY’

Big thanks AngryAnt and Big thanks the forum.
The bad point is that i lost 1 complete day on this thingie.

Have a nice day :slight_smile: