Search Unity

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

Discussion in 'Scripting' started by BangBangGame, Dec 21, 2014.

  1. BangBangGame

    BangBangGame

    Joined:
    Dec 21, 2014
    Posts:
    4
    Help. :)
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Underwater : MonoBehaviour {
    6.  
    7.     // Attach this script to main camera
    8.     // And Define variable underwaterLevel up
    9.     // to your water level or sea level Y-coordinate
    10.     public float underwaterLevel = 7;
    11.  
    12.     // These variable to store
    13.     // The scene default fog settings
    14.     private bool defaultFog = true;
    15.     private Color defaultFogColor;
    16.     private float defaultFogDensity;
    17.     private Material defaultSkybox;
    18.     private float defaultStartDistance;
    19.  
    20.     void Start () {
    21.         // store default fog setting
    22.         // we need to restore fog setting
    23.         // after we go to surface again
    24.         defaultFog = RenderSettings.fog;
    25.         defaultFogColor = RenderSettings.fogColor;
    26.         defaultFogDensity = RenderSettings.fogDensity;
    27.         defaultSkybox = RenderSettings.skybox;
    28.         defaultStartDistance = RenderSettings.fogStartDistance;
    29.      
    30.         // set the background color
    31.     }
    32.  
    33.     void Update () {
    34.         // check if we below the sea or water level
    35.         if (transform.position.y < underwaterLevel) {
    36.             // render new fog with blue color
    37.             // Or you can change the color to
    38.             // match your water
    39.             RenderSettings.fog = true;
    40.             RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
    41.             RenderSettings.fogDensity = 0.1f;
    42.             RenderSettings.fogStartDistance = 0.0f;
    43.          
    44.             // add this if you want to add blur effect to your underwater
    45.             // but first add Image Effect (Pro) Package to your project
    46.             // Add component Image Effect > Blur to Main camera
    47.             this.GetComponent<BlurEffect>().enabled = true;
    48.         } else {
    49.             // revert back to default setting
    50.             RenderSettings.fog = defaultFog;
    51.             RenderSettings.fogColor = defaultFogColor;
    52.             RenderSettings.fogDensity = defaultFogDensity;
    53.             RenderSettings.skybox = defaultSkybox;
    54.             RenderSettings.fogStartDistance = defaultStartDistance;
    55.          
    56.             // add this if you want to add blur effect to your underwater
    57.             // but first add Image Effect (Pro) Package to your project
    58.             // Add component Image Effect > Blur to Main camera
    59.             this.GetComponent<BlurEffect>().enabled = false;
    60.         }
    61.     }
    62. }
    63.  
     
    Last edited: Dec 22, 2014
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    193
    Can you specify which line is #59? If you're using a pre-made asset most likely you're missing something in the inspector
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.
     
  4. BangBangGame

    BangBangGame

    Joined:
    Dec 21, 2014
    Posts:
    4
    Crayz.

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

    BangBangGame

    Joined:
    Dec 21, 2014
    Posts:
    4
    How I can fih that?
     
  6. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Underwater : MonoBehaviour {
    5.    
    6.     // Attach this script to main camera
    7.     // And Define variable underwaterLevel up
    8.     // to your water level or sea level Y-coordinate
    9.     public float underwaterLevel = 7;
    10.    
    11.     // These variable to store
    12.     // The scene default fog settings
    13.     private bool defaultFog = true;
    14.     private Color defaultFogColor;
    15.     private float defaultFogDensity;
    16.     private Material defaultSkybox;
    17.     private float defaultStartDistance;
    18.  
    19.     // BlurEffect
    20.     private BlurEffect blurEffect;
    21.    
    22.     void Start () {
    23.         // store default fog setting
    24.         // we need to restore fog setting
    25.         // after we go to surface again
    26.         defaultFog = RenderSettings.fog;
    27.         defaultFogColor = RenderSettings.fogColor;
    28.         defaultFogDensity = RenderSettings.fogDensity;
    29.         defaultSkybox = RenderSettings.skybox;
    30.         defaultStartDistance = RenderSettings.fogStartDistance;
    31.        
    32.         // set the background color
    33.  
    34.         // Retrieve the BlurEffect component
    35.         blurEffect = this.GetComponent<BlurEffect>();
    36.  
    37.         if ( blurEffect == null ) {
    38.             Debug.LogError( "This controller doesn't have a BlurEffect component!" );
    39.         }
    40.     }
    41.    
    42.     void Update () {
    43.         // check if we below the sea or water level
    44.         if (transform.position.y < underwaterLevel) {
    45.             // render new fog with blue color
    46.             // Or you can change the color to
    47.             // match your water
    48.             RenderSettings.fog = true;
    49.             RenderSettings.fogColor = new Color(0, 0.4f, 0.7f, 0.6f);
    50.             RenderSettings.fogDensity = 0.1f;
    51.             RenderSettings.fogStartDistance = 0.0f;
    52.            
    53.             // add this if you want to add blur effect to your underwater
    54.             // but first add Image Effect (Pro) Package to your project
    55.             // Add component Image Effect > Blur to Main camera
    56.             if ( blurEffect != null ) {
    57.                 blurEffect.enabled = true;
    58.             }
    59.         }
    60.         else {
    61.             // revert back to default setting
    62.             RenderSettings.fog = defaultFog;
    63.             RenderSettings.fogColor = defaultFogColor;
    64.             RenderSettings.fogDensity = defaultFogDensity;
    65.             RenderSettings.skybox = defaultSkybox;
    66.             RenderSettings.fogStartDistance = defaultStartDistance;
    67.            
    68.             // add this if you want to add blur effect to your underwater
    69.             // but first add Image Effect (Pro) Package to your project
    70.             // Add component Image Effect > Blur to Main camera
    71.             if ( blurEffect != null ) {
    72.                 blurEffect.enabled = false;
    73.             }
    74.         }
    75.     }
    76. }
     
  7. BangBangGame

    BangBangGame

    Joined:
    Dec 21, 2014
    Posts:
    4
    Debug.LogError( "This controller doesn't have a BlurEffect component!" )
    Now what I can do? Maybe I can change effect?
     
  8. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You then need a script called 'BlurEffect' on your gameObject, because in your script there's the following piece of code:

    Code (CSharp):
    1. blurEffect = this.GetComponent<BlurEffect>();
    2.  
    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).