- Home /
setting double in editor vs setting it in script (the latter doesn't work)
Hi this question is propably verry simple and im just missing something,
how do i set a double variable in script to say 0.00434782 ?
It works if i set it in Editor but not when i set it in script
I tired :
public double AstroidFieldDensity = 4.347826086E-3;
public double AstroidFieldDensity = (double) 0.004347826086f;
public double AstroidFieldDensity = 0.004347826086;
Answer by Bunny83 · Dec 16, 2017 at 04:07 PM
Your code does work. However since your variable is public it is serialized. You assign your default value when the object instance is created. Field initializers are executed even before the constructor runs. However deserialization happens way after the object has been created. So if you don't want to set this value in the editor but only inside the script, make it private or add the NonSerialized attribute to your variable
private double AstroidFieldDensity = 4.347826086E-3;
// or
[System.NonSerialized]
public double AstroidFieldDensity = 4.347826086E-3;
However if you want the values to be editable in the editor you can't set them from script that way. You can overwrite the serialized value inside Awake or Start. However this would make it pointless to have it serialized.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can someone explain calling other scripts in C#? 2 Answers
Why a public varibale is not accsible inside Update() ? 1 Answer