- Home /
Underwater script and serialization
Hi everyone,
I'm new on Unity programming, and I'm trying to have some Underwater effects but I'm facing a problem because I need to serialize my code but even with the documentation I don't understand a thing!
My simple code is :
var underwaterLevel =80;
private var Fog = RenderSettings.fog;
private var FogColor = RenderSettings.fogColor;
private var FogDensity = RenderSettings.fogDensity;
function Start () {
}
function Update () {
if (transform.position.y < underwaterLevel){
RenderSettings.fog = true;
RenderSettings.fogColor = Color32(95, 160, 188, 128);
RenderSettings.fogDensity = 0.04;
}
}
and my error is :
UnityException: get_fog is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'UnderWater' on game object 'Main Camera'. See "Script Serialization" page in the Unity Manual for further details. UnderWater..ctor () (at Assets/Scripts/UnderWater.js:5) UnityEditorInternal.InternalEditorUtility:HierarchyWindowDrag(HierarchyProperty, Boolean, HierarchyDropMode) UnityEditor.DockArea:OnGUI()
So if someone could help me it would be really cool!
Thanks
Answer by tanoshimi · Jul 25, 2017 at 05:10 PM
I don't use Unityscript, but, at a guess:
var underwaterLevel =80;
private var Fog;
private var FogColor;
private var FogDensity;
function Start () {
Fog = RenderSettings.fog;
FogColor = RenderSettings.fogColor;
FogDensity = RenderSettings.fogDensity;
}
function Update () {
if (transform.position.y < underwaterLevel){
RenderSettings.fog = true;
RenderSettings.fogColor = Color32(95, 160, 188, 128);
RenderSettings.fogDensity = 0.04;
}
}
Your answer
Follow this Question
Related Questions
Bool array to binaryformatter method 1 Answer
Is it possible to create a custom gettter/setter on SerializedProperty ? 0 Answers
Serialized Fields go bye bye on Android 0 Answers
How do I save scripts as variables in the inspector? 0 Answers
EndOfStreamException : Failed to read past end of stream 1 Answer