0
\$\begingroup\$

Here's my code

[SerializeField]
GameObject FogEffect = null;

void Update()
{
    GetFogs();
}

void GetFogs()
{
    int env_idx = tzPlayInfo.Instance.ENV_01_IDX;
    //0: day, 1: night
    if (env_idx == 0 && FogEffect == null)
    {
        FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    } else if (env_idx == 1 && FogEffect == null)
    {
        FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    } else
    {
        Debug.Log("There's something wrong!");
    }
}

I'm changing my prefabs dynamically so when it is Evening I am using evening prefab then if it is Day I'm using my day prefab. So basically i need to do it on Update() function because I am making an Option that the user can enable and disable the FOG GameObject. Now my problem is that everytime it changes the environment (Day and Night) a

NullReferenceException

is popping out but after that it will get the Fog GameObject again.

How can I solve this error.

EDITTED: Here's the whole code for my OptionSetting.cs

using System;

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.ImageEffects; using System.Linq;

public class MC_SettingBehaviour : MonoBehaviour {

[SerializeField]
GameObject[] CameraEffectComponents = new GameObject[43];

bool foundAllComponents = false;

[SerializeField]
GameObject FogEffectMorning = null;

[SerializeField]
GameObject FogEffectEvening = null;
bool fog = false;

private void Start()
{
    if (PlayerPrefs.GetInt(OptionPopup.LiveStreaming_) == 1)
    {
        this.enabled = false;
    } else
    {
        StartCoroutine(CameraComponents());
    }
}

void Update()
{
    if (!fog)
    {
        GetFogs();
        Debug.Log("BOOLEAN FOG :" + fog);
    }

    //if the camera components are all found
    if (foundAllComponents)
    {
        //Camera Option
        OptionCameraOnAndOff();
    }
    else
    {
        Debug.Log("Couldn't find yet any components on camera please wait...");
    }
}

IEnumerator CameraComponents()
{

    CameraEffectComponents[0] = transform.Find("GO Root/Camera/AIR_1(Clone)/GameObject/Camera").gameObject;
    CameraEffectComponents[1] = transform.Find("GO Root/Camera/AIR_2(Clone)/Camera").gameObject;
    CameraEffectComponents[2] = transform.Find("GO Root/Camera/Paddock_1(Clone)/CameraPos/GameObject/Camera").gameObject;
    CameraEffectComponents[3] = transform.Find("GO Root/Camera/TV_1_lead(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[4] = transform.Find("GO Root/Camera/TV_2_far(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[5] = transform.Find("GO Root/Camera/TV_2_lead(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[6] = transform.Find("GO Root/Camera/LAST_SPURT_1(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[7] = transform.Find("GO Root/Camera/Test30(Clone)/Poss/Camera").gameObject;

    CameraEffectComponents[8] = transform.Find("GO Root/Camera/Test01(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[9] = transform.Find("GO Root/Camera/Test02(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[10] = transform.Find("GO Root/Camera/Test03(Clone)/Poss/Camera").gameObject;

    CameraEffectComponents[11] = transform.Find("GO Root/Camera/Test04(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[12] = transform.Find("GO Root/Camera/Test05(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[13] = transform.Find("GO Root/Camera/Test06(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[14] = transform.Find("GO Root/Camera/Test07(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[15] = transform.Find("GO Root/Camera/Test08(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[16] = transform.Find("GO Root/Camera/Test09(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[17] = transform.Find("GO Root/Camera/Test10(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[18] = transform.Find("GO Root/Camera/Test11(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[19] = transform.Find("GO Root/Camera/Test12(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[20] = transform.Find("GO Root/Camera/Test13(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[21] = transform.Find("GO Root/Camera/Test14(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[22] = transform.Find("GO Root/Camera/Test15(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[23] = transform.Find("GO Root/Camera/Test16(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[24] = transform.Find("GO Root/Camera/Test17(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[25] = transform.Find("GO Root/Camera/Test18(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[26] = transform.Find("GO Root/Camera/Test19(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[27] = transform.Find("GO Root/Camera/Test20(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[28] = transform.Find("GO Root/Camera/Test21(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[29] = transform.Find("GO Root/Camera/Test22(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[30] = transform.Find("GO Root/Camera/Test23(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[31] = transform.Find("GO Root/Camera/Test24(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[32] = transform.Find("GO Root/Camera/Test25(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[33] = transform.Find("GO Root/Camera/Test26(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[34] = transform.Find("GO Root/Camera/Test27(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[34] = transform.Find("GO Root/Camera/Test28(Clone)/Poss/Camera").gameObject;
    CameraEffectComponents[35] = transform.Find("GO Root/Camera/Test29(Clone)/Poss/Camera").gameObject;

    CameraEffectComponents[36] = transform.Find("GO Root/Camera/HORSE_JOCKEY(Clone)").gameObject;

    CameraEffectComponents[37] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_1(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[38] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_2(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[39] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_3(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[40] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/FRONT_1(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[41] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_1(Clone)/Pos/Camera").gameObject;
    CameraEffectComponents[42] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_2(Clone)/Pos/Camera").gameObject;

    //lets use a lambada expression here for declaring a null
    if (CameraEffectComponents.Any(e => e != null))
    {
        foundAllComponents = true;
    } else
    {
        foundAllComponents = false;
    }
    yield return null;
}

void GetFogs()
{
    int env_idx = tzPlayInfo.Instance.ENV_01_IDX;
    //0: day, 1: night
    try
    {
        if (env_idx == 0 && FogEffectMorning == null)
        {
            fog = false;
            FogEffectMorning = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
            Debug.Log("Its morning must get the Fog Morning Effect");
            //Fog Option

            OptionFogMorningOnAndOff();
            fog = true;
        }
        else if (env_idx == 1 && FogEffectEvening == null)
        {
            fog = false;
            FogEffectEvening = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
            Debug.Log("Its morning must get the Night Morning Effect");
            //Fog Option

            OptionFogEveningOnAndOff();
            fog = true;
        }
        else
        {
            Debug.Log("There's something wrong!");
        }
    }
    catch (Exception e)
    {
        Debug.Log("Opps something is wrong :" + e.ToString());
    }

}

void OptionCameraOnAndOff()
{
    foreach (GameObject cam in CameraEffectComponents)
    {
        if (PlayerPrefs.GetInt(OptionPopup.CameraOptionSaving) == 1)
        {
            if (cam.GetComponent<Bloom>() != null)
            {
                cam.GetComponent<Bloom>().enabled = true;
            }

            if (cam.GetComponent<DepthOfFieldDeprecated>() != null)
            {
                cam.GetComponent<DepthOfFieldDeprecated>().enabled = true;
            }

            if (cam.GetComponent<DepthOfField>() != null)
            {
                cam.GetComponent<DepthOfField>().enabled = true;
            }

        }
        else
        {

            if (cam.GetComponent<Bloom>() != null)
            {
                cam.GetComponent<Bloom>().enabled = false;
            }

            if (cam.GetComponent<DepthOfFieldDeprecated>() != null)
            {
                cam.GetComponent<DepthOfFieldDeprecated>().enabled = false;
            }

            if (cam.GetComponent<DepthOfField>() != null)
            {
                cam.GetComponent<DepthOfField>().enabled = false;
            }
        }
    }
}

void OptionFogMorningOnAndOff()
{
    try
    {
        if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
        {
            FogEffectMorning.SetActive(true);
        }
        else
        {
            FogEffectMorning.SetActive(false);
        }

    }
    catch (Exception e)
    {
        Debug.Log("Something is wrong here :" + e.ToString());
    }

}

void OptionFogEveningOnAndOff()
{
    try
    {
        if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
        {
            FogEffectEvening.SetActive(true);
        }
        else
        {
            FogEffectEvening.SetActive(false);
        }

    }
    catch (Exception e)
    {
        Debug.Log("Something is wrong here :" + e.ToString());
    }
}
}
\$\endgroup\$
  • \$\begingroup\$ This may have something to do with your script execution order. I don’t know how your project is setup but it sounds like it’s null for a frame then obtains it’s reference? Hard to say without seeing everything. On a side note, why not use events to achieve this behaviour? Rather than checking in the update method. \$\endgroup\$ – Savlon Feb 22 '18 at 12:56
  • \$\begingroup\$ Can you show us where you are resetting FogEffect to null in order for either of those conditions to become true? \$\endgroup\$ – Kyy13 Feb 22 '18 at 15:16
  • \$\begingroup\$ @Kyy13 I'll edit my question :) \$\endgroup\$ – NoobProgrammer Feb 23 '18 at 1:28
  • \$\begingroup\$ @Savlon . How can i show you my setup ? In the heirarchy? sorry for my bad english \$\endgroup\$ – NoobProgrammer Feb 23 '18 at 1:34
  • \$\begingroup\$ Ahh ok I see. Disregard my comment about script execution order because it's all in the same script. Ok so, maybe your issue has to do with the "Find" method. This method is slow and it's possible it's taking too long to find the fog gameobject prior to the frame ending. You're calling the find method every frame which is costly and unnecessary. Try storing the FogEffect alternatives (Day/night) in their own reference and change between them instead of using "Find" \$\endgroup\$ – Savlon Feb 23 '18 at 10:00
0
\$\begingroup\$

I couldn't do it on the Find method so basically what i did is make a script on the fog(evening) fog(morning) and put this script so that they can be still disabled and enabled .

void Update ()
{
    if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
    {
        this.GetComponent<MeshRenderer>().enabled = true;
    }
    else
    {
        this.GetComponent<MeshRenderer>().enabled = false;
    }
}

I just turn on and off the mesh renderer. Hahaha thanks guys

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.