using UnityEngine;
using System.Collections;
public class Underwater : MonoBehaviour {
// Attach this script to main camera
// And Define variable underwaterLevel up
// to your water level or sea level Y-coordinate
public float underwaterLevel = 7;
// These variable to store
// The scene default fog settings
private bool defaultFog = true;
private Color defaultFogColor;
private float defaultFogDensity;
private Material defaultSkybox;
private float defaultStartDistance;
void Start () {
// store default fog setting
// we need to restore fog setting
// after we go to surface again
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
defaultStartDistance = RenderSettings.fogStartDistance;
// set the background color
}
void Update () {
// check if we below the sea or water level
if (transform.position.y < underwaterLevel) {
// render new fog with blue color
// Or you can change the color to
// match your water
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.1f;
RenderSettings.fogStartDistance = 0.0f;
// add this if you want to add blur effect to your underwater
// but first add Image Effect (Pro) Package to your project
// Add component Image Effect > Blur to Main camera
this.GetComponent<BlurEffect>().enabled = true;
} else {
// revert back to default setting
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
RenderSettings.skybox = defaultSkybox;
RenderSettings.fogStartDistance = defaultStartDistance;
// add this if you want to add blur effect to your underwater
// but first add Image Effect (Pro) Package to your project
// Add component Image Effect > Blur to Main camera
this.GetComponent<BlurEffect>().enabled = false;
}
}
}
Would be easier with the correct lines in your code. Iâd guess you do not have any âBlurEffectâ on your object or the defaultSkyBox is not assigned.
Your code is running every update btw, which is not that great in regards to setting those values every frame.
using UnityEngine;
using System.Collections;
public class Underwater : MonoBehaviour {
// Attach this script to main camera
// And Define variable underwaterLevel up
// to your water level or sea level Y-coordinate
public float underwaterLevel = 7;
// These variable to store
// The scene default fog settings
private bool defaultFog = true;
private Color defaultFogColor;
private float defaultFogDensity;
private Material defaultSkybox;
private float defaultStartDistance;
// BlurEffect
private BlurEffect blurEffect;
void Start () {
// store default fog setting
// we need to restore fog setting
// after we go to surface again
defaultFog = RenderSettings.fog;
defaultFogColor = RenderSettings.fogColor;
defaultFogDensity = RenderSettings.fogDensity;
defaultSkybox = RenderSettings.skybox;
defaultStartDistance = RenderSettings.fogStartDistance;
// set the background color
// Retrieve the BlurEffect component
blurEffect = this.GetComponent<BlurEffect>();
if ( blurEffect == null ) {
Debug.LogError( "This controller doesn't have a BlurEffect component!" );
}
}
void Update () {
// check if we below the sea or water level
if (transform.position.y < underwaterLevel) {
// render new fog with blue color
// Or you can change the color to
// match your water
RenderSettings.fog = true;
RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
RenderSettings.fogDensity = 0.1f;
RenderSettings.fogStartDistance = 0.0f;
// add this if you want to add blur effect to your underwater
// but first add Image Effect (Pro) Package to your project
// Add component Image Effect > Blur to Main camera
if ( blurEffect != null ) {
blurEffect.enabled = true;
}
}
else {
// revert back to default setting
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
RenderSettings.skybox = defaultSkybox;
RenderSettings.fogStartDistance = defaultStartDistance;
// add this if you want to add blur effect to your underwater
// but first add Image Effect (Pro) Package to your project
// Add component Image Effect > Blur to Main camera
if ( blurEffect != null ) {
blurEffect.enabled = false;
}
}
}
}
You then need a script called âBlurEffectâ on your gameObject, because in your script thereâs the following piece of code:
blurEffect = this.GetComponent<BlurEffect>();
When there isnât any component of type âBlueEffectâ, the variable âblurEffectâ will remain null. Thatâs why the message is shown which @toreau has added to your script.
I assume âBlurEffectâ is the image effect provided by Unity with the standard assets, so it shouldnât be any problem to add the script (most likely requires Unity Pro license in order to work).