- Home /
Found the answer but in a different way. Unexplanable
Dynamically Changing GameObject makes NullReferenceException(C# Unity)
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
REVISION:
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
//Let's put it here for the reference so that whenever the environment changes it will still reference
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
//Let's put it here for the reference so that whenever the environment changes it will still reference
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());
}
}
}
Answer by Harinezumi · Feb 22, 2018 at 09:41 AM
If transform.Find()
doesn't find anything, it returns null
, if you try to access anything on null
, you'll get a "NullReferenceException". So check the result of transform.Find()
before trying to get the game object from it.
Additionally, if there is no fog in the scene, this code will run transform.Find()
on every Update()
slowing your game a lot, and if there is one, you will get "There's something wrong!" (because now both previous conditions are false). I would recommend just assigning in the Editor the day and night fog game objects directly, and switch on the environment index, e.g.:
[SerializeField] GameObject dayFogEffect = null;
[SerializeField] GameObject nightFogEffect = null;
private GameObject currentFogEffect;
private void Update () {
SelectCurrentFog();
}
private void SelectCurrentFog() {
switch (tzPlayInfo.Instance.ENV_01_IDX) {
case 0: currentFogEffect = dayFogEffect; break;
case 1: currentFogEffect = nightFogEffect; break;
default: Debug.Log("There's something wrong"); currentFogEffect = null; break;
}
}
I could not assign it by dragging it from the inspector because it changes dynamically . When it is evening it will change its prefabs the same as when it's morning it will instantiate a different prefab. Would you $$anonymous$$d taking a look at my new reconstruct code. I will edit my question