I have written a script from an example I found on the internet, my problem is that the script does not change the fog color and I cannot figure out why.
using UnityEngine;
using System.Collections;
public class UnderWaterEffect : MonoBehaviour {
//Define variable
public GameObject waterLevel;
//The scene's default fog settings
private bool defaultFog = RenderSettings.fog;
private Color defaultFogColor = RenderSettings.fogColor;
private float defaultFogDensity = RenderSettings.fogDensity;
void Update() {
if (GetComponent<Camera>().transform.position.y < waterLevel.transform.position.y) {
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.04f;
}
else {
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
}
}
}
You can’t initialize the RenderSettings when declaring the variables, its causing an error in 5.3.5f1 on startup. To fix this, simply set the default values in Start ().
Apparently Unity changed something in the 5x cycle that causes the error, but I honestly don’t know more than that…
using UnityEngine;
using System.Collections;
public class UnderWaterEffect : MonoBehaviour {
//Define variable
public GameObject waterLevel;
//The scene's default fog settings
private bool defaultFog;
private Color defaultFogColor;
private float defaultFogDensity;
void Start ()
{
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
}
void Update()
{
if (GetComponent<Camera>().transform.position.y < waterLevel.transform.position.y) {
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.04f;
}
else {
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
}
}
}