Hello all ![]()
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 ![]()
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 ![]()
Have a nice day