object reference not set to an instance of an object. c#

Help. :slight_smile:

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;
        }
    }
}

Can you specify which line is #59? If you’re using a pre-made asset most likely you’re missing something in the inspector

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.

Crayz.

Sorry, I had seen that I uploaded incomplete writings . Now Patarkit as pataisiti or get the effect.

How I can fih that?

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;
            }
        }
    }
}

Debug.LogError( “This controller doesn’t have a BlurEffect component!” )
Now what I can do? Maybe I can change effect?

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).