Search Unity

NEWLY BUILD APP CRASHES BECAUSE OF TRANSFORM.FIND()

Discussion in 'Scripting' started by Ginxx009, Feb 20, 2018.

  1. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I've got this error on my logcat

    02-20 15:55:17.819 2289-2554/system_process W/InputDispatcher: channel '4ea1a178 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
    02-20 15:55:17.819 2289-2554/system_process E/InputDispatcher: channel '4ea1a178 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
    02-20 15:55:17.819 2289-2680/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '4ea1a178 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity (server)'
    02-20 15:55:17.819 2289-2680/system_process I/WindowState: WIN DEATH: Window{4ea1a178 u0 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity}
    02-20 15:55:17.819 2289-2680/system_process W/WindowManager: Force-removing child win Window{4eb62a9c u0 SurfaceView} from container Window{4ea1a178 u0 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity}
    02-20 15:55:17.819 2289-2708/system_process W/ActivityManager: Force removing ActivityRecord{4e99fcc4 u0 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity t11}: app died, no saved state


    Why did i say because of my transform.Find() function? Because i do some debugging and found out that whenever i remove my code


    What i am doing here is that i need to change the whats inside of my FogEffect GameObject when it's change from (DAY) to (NIGHT) . When it's day i need to put the

    Code (CSharp):
    1. FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    and when it's night

    Code (CSharp):
    1. FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    And yeah it's kind of working on the inspector but when i'm trying to build it as APK it crashes when i run the APK.

    Here's my full code.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityStandardAssets.ImageEffects;
    6. using System.Linq;
    7. public class MC_SettingBehaviour : MonoBehaviour {
    8.     [SerializeField]
    9.     GameObject CameraScriptsAir_1, CameraScripstAir_2;
    10.     [SerializeField]
    11.     GameObject Paddocks;
    12.     [SerializeField]
    13.     GameObject Jockey;
    14.     [SerializeField]
    15.     GameObject[] CameraScriptsTV = new GameObject[3];
    16.     [SerializeField]
    17.     GameObject[] TwoScript = new GameObject[2];
    18.     [SerializeField]
    19.     GameObject[] Tests1 = new GameObject[3];
    20.     [SerializeField]
    21.     GameObject[] Tests2 = new GameObject[26];
    22.     [SerializeField]
    23.     GameObject[] follow_camera_focus = new GameObject[6];
    24.     [SerializeField]
    25.     GameObject FogEffect = null;
    26.     int maxCameraScriptTV = 3;
    27.     int maxTests1 = 3;
    28.     int maxTests2 = 26;
    29.     int maxfcf = 6;
    30.     bool foundAllComponents = false;
    31.     bool isNight = false;
    32.     Bloom bloom;
    33.     DepthOfFieldDeprecated DOFD;
    34.     private void Start()
    35.     {
    36.         if (tzPlayInfo.Instance.BLOCK_GAME_FLOW == false)
    37.         {
    38.             if (PlayerPrefs.GetInt(OptionPopup.LiveStreaming_) == 1)
    39.             {
    40.                 this.enabled = false;
    41.             } else
    42.             {
    43.                 StartCoroutine(CameraComponents());
    44.             }
    45.         }
    46.     }
    47.     private void Update()
    48.     {
    49.         //Needs to iterate because of the ENV_01_IDX
    50.         try
    51.         {
    52.             StartCoroutine(GetFogComponent());
    53.             if (isNight)
    54.             {
    55.                 FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    56.              
    57.             }
    58.             else
    59.             {
    60.                 FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    61.              
    62.             }
    63.         }
    64.         catch (Exception e)
    65.         {
    66.             Debug.Log("Couldn't Find Any Fog GameObject" + e);
    67.         }
    68.         //Fog
    69.         if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
    70.         {
    71.             if (FogEffect != null)
    72.             {
    73.                 FogEffect.SetActive(true);
    74.             }
    75.             else
    76.             {
    77.                 Debug.Log("Nothing to Disable");
    78.             }
    79.         }
    80.         else
    81.         {
    82.             if (FogEffect != null)
    83.             {
    84.                 FogEffect.SetActive(false);
    85.             }
    86.             else
    87.             {
    88.                 Debug.Log("Nothing to Disable");
    89.             }
    90.         }
    91.         //if the camera components are all found
    92.         if (foundAllComponents)
    93.         {
    94.             //save camera
    95.             if (PlayerPrefs.GetInt(OptionPopup.CameraOptionSaving) == 1)
    96.             {
    97.                 CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
    98.                 CameraScriptsAir_1.GetComponent<Bloom>().enabled = true;
    99.                 CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = true;
    100.                 CameraScripstAir_2.GetComponent<Bloom>().enabled = true;
    101.                 foreach (GameObject camScriptTV in CameraScriptsTV)
    102.                 {
    103.                     camScriptTV.GetComponent<Bloom>().enabled = true;
    104.                 }
    105.                 foreach (GameObject two in TwoScript)
    106.                 {
    107.                     two.GetComponent<DepthOfFieldDeprecated>().enabled = true;
    108.                     two.GetComponent<Bloom>().enabled = true;
    109.                 }
    110.                 foreach (GameObject test1 in Tests1)
    111.                 {
    112.                     test1.GetComponent<DepthOfFieldDeprecated>().enabled = true;
    113.                 }
    114.                 foreach (GameObject test2 in Tests2)
    115.                 {
    116.                     test2.GetComponent<Bloom>().enabled = true;
    117.                 }
    118.                 foreach (GameObject followcamerafocus in follow_camera_focus)
    119.                 {
    120.                     DOFD = GetComponent<DepthOfFieldDeprecated>();
    121.                     bloom = GetComponent<Bloom>();
    122.                     if (DOFD != null )
    123.                     {
    124.                         followcamerafocus.GetComponent<DepthOfField>().enabled = true;
    125.                     }
    126.                     if (bloom != null)
    127.                     {
    128.                         followcamerafocus.GetComponent<Bloom>().enabled = true;
    129.                     }
    130.                 }
    131.             }
    132.             else
    133.             {
    134.                 CameraScriptsAir_1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
    135.                 CameraScriptsAir_1.GetComponent<Bloom>().enabled = false;
    136.                 CameraScripstAir_2.GetComponent<DepthOfFieldDeprecated>().enabled = false;
    137.                 CameraScripstAir_2.GetComponent<Bloom>().enabled = false;
    138.                 foreach (GameObject camScriptTV in CameraScriptsTV)
    139.                 {
    140.                     camScriptTV.GetComponent<Bloom>().enabled = false;
    141.                 }
    142.                 foreach (GameObject two in TwoScript)
    143.                 {
    144.                     two.GetComponent<DepthOfFieldDeprecated>().enabled = false;
    145.                     two.GetComponent<Bloom>().enabled = false;
    146.                 }
    147.                 foreach (GameObject test1 in Tests1)
    148.                 {
    149.                     test1.GetComponent<DepthOfFieldDeprecated>().enabled = false;
    150.                 }
    151.                 foreach (GameObject test2 in Tests2)
    152.                 {
    153.                     test2.GetComponent<Bloom>().enabled = false;
    154.                 }
    155.                 foreach (GameObject followcamerafocus in follow_camera_focus)
    156.                 {
    157.                     DOFD = GetComponent<DepthOfFieldDeprecated>();
    158.                     bloom = GetComponent<Bloom>();
    159.                     if (DOFD != null)
    160.                     {
    161.                         followcamerafocus.GetComponent<DepthOfField>().enabled = false;
    162.                     }
    163.                     if (bloom != null)
    164.                     {
    165.                         followcamerafocus.GetComponent<Bloom>().enabled = false;
    166.                     }
    167.                 }
    168.             }
    169.         }
    170.     }
    171.     IEnumerator CameraComponents()
    172.     {
    173.         CameraScriptsAir_1 = transform.Find("GO Root/Camera/AIR_1(Clone)/GameObject/Camera").gameObject;
    174.         CameraScripstAir_2 = transform.Find("GO Root/Camera/AIR_2(Clone)/Camera").gameObject;
    175.         Paddocks = transform.Find("GO Root/Camera/Paddock_1(Clone)/CameraPos/GameObject/Camera").gameObject;
    176.      
    177.         CameraScriptsTV[0] = transform.Find("GO Root/Camera/TV_1_lead(Clone)/Poss/Camera").gameObject;
    178.         CameraScriptsTV[1] = transform.Find("GO Root/Camera/TV_2_far(Clone)/Poss/Camera").gameObject;
    179.         CameraScriptsTV[2] = transform.Find("GO Root/Camera/TV_2_lead(Clone)/Poss/Camera").gameObject;
    180.         TwoScript[0] = transform.Find("GO Root/Camera/LAST_SPURT_1(Clone)/Pos/Camera").gameObject;
    181.         TwoScript[1] = transform.Find("GO Root/Camera/Test30(Clone)/Poss/Camera").gameObject;
    182.         Tests1[0] = transform.Find("GO Root/Camera/Test01(Clone)/Poss/Camera").gameObject;
    183.         Tests1[1] = transform.Find("GO Root/Camera/Test02(Clone)/Poss/Camera").gameObject;
    184.         Tests1[2] = transform.Find("GO Root/Camera/Test03(Clone)/Poss/Camera").gameObject;
    185.         Tests2[0] = transform.Find("GO Root/Camera/Test04(Clone)/Poss/Camera").gameObject;
    186.         Tests2[1] = transform.Find("GO Root/Camera/Test05(Clone)/Poss/Camera").gameObject;
    187.         Tests2[2] = transform.Find("GO Root/Camera/Test06(Clone)/Poss/Camera").gameObject;
    188.         Tests2[3] = transform.Find("GO Root/Camera/Test07(Clone)/Poss/Camera").gameObject;
    189.         Tests2[4] = transform.Find("GO Root/Camera/Test08(Clone)/Poss/Camera").gameObject;
    190.         Tests2[5] = transform.Find("GO Root/Camera/Test09(Clone)/Poss/Camera").gameObject;
    191.         Tests2[6] = transform.Find("GO Root/Camera/Test10(Clone)/Poss/Camera").gameObject;
    192.         Tests2[7] = transform.Find("GO Root/Camera/Test11(Clone)/Poss/Camera").gameObject;
    193.         Tests2[8] = transform.Find("GO Root/Camera/Test12(Clone)/Poss/Camera").gameObject;
    194.         Tests2[9] = transform.Find("GO Root/Camera/Test13(Clone)/Poss/Camera").gameObject;
    195.         Tests2[10] = transform.Find("GO Root/Camera/Test14(Clone)/Poss/Camera").gameObject;
    196.         Tests2[11] = transform.Find("GO Root/Camera/Test15(Clone)/Poss/Camera").gameObject;
    197.         Tests2[12] = transform.Find("GO Root/Camera/Test16(Clone)/Poss/Camera").gameObject;
    198.         Tests2[13] = transform.Find("GO Root/Camera/Test17(Clone)/Poss/Camera").gameObject;
    199.         Tests2[14] = transform.Find("GO Root/Camera/Test18(Clone)/Poss/Camera").gameObject;
    200.         Tests2[15] = transform.Find("GO Root/Camera/Test19(Clone)/Poss/Camera").gameObject;
    201.         Tests2[16] = transform.Find("GO Root/Camera/Test20(Clone)/Poss/Camera").gameObject;
    202.         Tests2[17] = transform.Find("GO Root/Camera/Test21(Clone)/Poss/Camera").gameObject;
    203.         Tests2[18] = transform.Find("GO Root/Camera/Test22(Clone)/Poss/Camera").gameObject;
    204.         Tests2[19] = transform.Find("GO Root/Camera/Test23(Clone)/Poss/Camera").gameObject;
    205.         Tests2[20] = transform.Find("GO Root/Camera/Test24(Clone)/Poss/Camera").gameObject;
    206.         Tests2[21] = transform.Find("GO Root/Camera/Test25(Clone)/Poss/Camera").gameObject;
    207.         Tests2[22] = transform.Find("GO Root/Camera/Test26(Clone)/Poss/Camera").gameObject;
    208.         Tests2[23] = transform.Find("GO Root/Camera/Test27(Clone)/Poss/Camera").gameObject;
    209.         Tests2[24] = transform.Find("GO Root/Camera/Test28(Clone)/Poss/Camera").gameObject;
    210.         Tests2[25] = transform.Find("GO Root/Camera/Test29(Clone)/Poss/Camera").gameObject;
    211.         Jockey = transform.Find("GO Root/Camera/HORSE_JOCKEY(Clone)").gameObject;
    212.         follow_camera_focus[0] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_1(Clone)/Pos/Camera").gameObject;
    213.         follow_camera_focus[1] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_2(Clone)/Pos/Camera").gameObject;
    214.         follow_camera_focus[2] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_3(Clone)/Pos/Camera").gameObject;
    215.         follow_camera_focus[3] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/FRONT_1(Clone)/Pos/Camera").gameObject;
    216.         follow_camera_focus[4] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_1(Clone)/Pos/Camera").gameObject;
    217.         follow_camera_focus[5] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_2(Clone)/Pos/Camera").gameObject;
    218.         //lets use a lambada expression here for declaring a null
    219.         if (CameraScriptsAir_1 && CameraScripstAir_2 && Jockey && Paddocks != null && CameraScriptsTV.Any(e => e != null)
    220.             && TwoScript.Any(e => e != null) && Tests1.Any(e =>e != null) && Tests2.Any(e => e != null) && follow_camera_focus.Any(e => e != null))
    221.         {
    222.             foundAllComponents = true;
    223.         } else
    224.         {
    225.             foundAllComponents = false;
    226.         }
    227.         yield return null;
    228.     }
    229.     IEnumerator GetFogComponent()
    230.     {
    231.         try
    232.         {
    233.             int env_idx = tzPlayInfo.Instance.ENV_01_IDX;
    234.             if (env_idx == 0)
    235.             {
    236.                 isNight = false;
    237.             }
    238.             else
    239.             {
    240.                 isNight = true;
    241.             }
    242.         }
    243.         catch (Exception e)
    244.         {
    245.             Debug.Log("Could not find Fog" + e);
    246.         }
    247.         yield return null;
    248.     }
    249. }
     
    Last edited: Feb 20, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Please refer to this page for posting code properly/nicely on the forums: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Well your coroutine doesn't really make sense, as you yield just at the end?

    Also, you can try to find a game object (though I think you would be better to link it in the inspector), but let's say you did try to find it, check if the return value is null instead of try catch?

    I don't think not finding it should crash? However, that might also mean I have no idea what's wrong :)
     
  3. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    But by just removing this entire line of code
    Code (CSharp):
    1. FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    and

    Code (CSharp):
    1. FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    Runs my APK or should i say it does not crash..
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I honestly don't understand the need for all those transform.find nor that huge chunk of code where you try to populate your array.

    Your update code feels unnecessary as "Update", but it's possible I don't understand what you are doing, it's just Update is a once a frame thing, and usually the need to activate or enabled things on such a scale isn't needed per frame.

    Your logcat doesn't seem to show the error part. Usually you should see some E sections for the errors, Generally when I see a crash that is what I would see in my logcat files.
     
    Last edited: Feb 20, 2018
  5. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    To filter logcat to just show Unity messages, use "adb logcat -s Unity"

    You might want to also include ActivityManager to show more detailed crash info: "adb logcat -s Unity ActivityManager"

    Either way, do those transform.Find statements work in Play mode? Honestly, finding assets by name is just asking for trouble because it's so incredibly fragile and will break if you rename anything, or restructure any objects in the hierarchy.

    I'd suggest either linking to an instance, or link to a prefab to instantiate on load.
     
  6. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Just what @BlackPete said... It's a hell lot of trouble to maintain that code. Populate the arrays via inspector, that's alot cleaner and alot more flexible. A simple typo or renaming could mess your application up which is really not desired, it's no surprise you're running into issues (not saying a crash is okay, but ... you know).
     
  7. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I am doing it on Update function because what i am doing there is like an Option of Fog Effect. So when the user want's to enable or disable it while he is playing so he can do it real time. Something like that.
     
  8. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Yep it works in Play mode just when i build only on APK it crashes.
     
  9. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I couldn't do it in array form because they're changing dynamically whenever the time passes something like when its morning the arrangment of my prefab changes also in afternoon and evening environment so i prefer using transform.Find()
     
  10. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just change the array as time passes whenever you update the arrangement. Running transform.Find all the time like this is error prone and performs terribly. Doing all those GetComponent calls every update also performs terribly. This might be the most inefficient code I've ever seen posted on the forum, like you've decided to try to eat soup using a shovel. I don't mean that as any kind of attack on you, but as a suggestion to completely rethink what you're trying to do.
     
  11. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    Could you help me out sir what remedy i could use to enable and disable those gameobjects when the user want to turn it off?
     
  12. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I changed my reconstruct my code here it is :

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityStandardAssets.ImageEffects;
    7. using System.Linq;
    8. public class MC_SettingBehaviour : MonoBehaviour {
    9.     [SerializeField]
    10.     GameObject[] CameraEffectComponents = new GameObject[43];
    11.     bool foundAllComponents = false;
    12.     [SerializeField]
    13.     GameObject FogEffect = null;
    14.    
    15.     private void Start()
    16.     {
    17.         if (PlayerPrefs.GetInt(OptionPopup.LiveStreaming_) == 1)
    18.         {
    19.             this.enabled = false;
    20.         } else
    21.         {
    22.             StartCoroutine(CameraComponents());
    23.         }
    24.     }
    25.     void Update()
    26.     {
    27.         GetFogs();
    28.         //if the camera components are all found
    29.         if (foundAllComponents)
    30.         {
    31.             //Camera Option
    32.             OptionCameraOnAndOff();
    33.         }
    34.         else
    35.         {
    36.             Debug.Log("Couldn't find yet any components on camera please wait...");
    37.         }
    38.         //Fog Option
    39.         OptionFogOnAndOff();
    40.     }
    41.     IEnumerator CameraComponents()
    42.     {
    43.         CameraEffectComponents[0] = transform.Find("GO Root/Camera/AIR_1(Clone)/GameObject/Camera").gameObject;
    44.         CameraEffectComponents[1] = transform.Find("GO Root/Camera/AIR_2(Clone)/Camera").gameObject;
    45.         CameraEffectComponents[2] = transform.Find("GO Root/Camera/Paddock_1(Clone)/CameraPos/GameObject/Camera").gameObject;
    46.         CameraEffectComponents[3] = transform.Find("GO Root/Camera/TV_1_lead(Clone)/Poss/Camera").gameObject;
    47.         CameraEffectComponents[4] = transform.Find("GO Root/Camera/TV_2_far(Clone)/Poss/Camera").gameObject;
    48.         CameraEffectComponents[5] = transform.Find("GO Root/Camera/TV_2_lead(Clone)/Poss/Camera").gameObject;
    49.         CameraEffectComponents[6] = transform.Find("GO Root/Camera/LAST_SPURT_1(Clone)/Pos/Camera").gameObject;
    50.         CameraEffectComponents[7] = transform.Find("GO Root/Camera/Test30(Clone)/Poss/Camera").gameObject;
    51.         CameraEffectComponents[8] = transform.Find("GO Root/Camera/Test01(Clone)/Poss/Camera").gameObject;
    52.         CameraEffectComponents[9] = transform.Find("GO Root/Camera/Test02(Clone)/Poss/Camera").gameObject;
    53.         CameraEffectComponents[10] = transform.Find("GO Root/Camera/Test03(Clone)/Poss/Camera").gameObject;
    54.         CameraEffectComponents[11] = transform.Find("GO Root/Camera/Test04(Clone)/Poss/Camera").gameObject;
    55.         CameraEffectComponents[12] = transform.Find("GO Root/Camera/Test05(Clone)/Poss/Camera").gameObject;
    56.         CameraEffectComponents[13] = transform.Find("GO Root/Camera/Test06(Clone)/Poss/Camera").gameObject;
    57.         CameraEffectComponents[14] = transform.Find("GO Root/Camera/Test07(Clone)/Poss/Camera").gameObject;
    58.         CameraEffectComponents[15] = transform.Find("GO Root/Camera/Test08(Clone)/Poss/Camera").gameObject;
    59.         CameraEffectComponents[16] = transform.Find("GO Root/Camera/Test09(Clone)/Poss/Camera").gameObject;
    60.         CameraEffectComponents[17] = transform.Find("GO Root/Camera/Test10(Clone)/Poss/Camera").gameObject;
    61.         CameraEffectComponents[18] = transform.Find("GO Root/Camera/Test11(Clone)/Poss/Camera").gameObject;
    62.         CameraEffectComponents[19] = transform.Find("GO Root/Camera/Test12(Clone)/Poss/Camera").gameObject;
    63.         CameraEffectComponents[20] = transform.Find("GO Root/Camera/Test13(Clone)/Poss/Camera").gameObject;
    64.         CameraEffectComponents[21] = transform.Find("GO Root/Camera/Test14(Clone)/Poss/Camera").gameObject;
    65.         CameraEffectComponents[22] = transform.Find("GO Root/Camera/Test15(Clone)/Poss/Camera").gameObject;
    66.         CameraEffectComponents[23] = transform.Find("GO Root/Camera/Test16(Clone)/Poss/Camera").gameObject;
    67.         CameraEffectComponents[24] = transform.Find("GO Root/Camera/Test17(Clone)/Poss/Camera").gameObject;
    68.         CameraEffectComponents[25] = transform.Find("GO Root/Camera/Test18(Clone)/Poss/Camera").gameObject;
    69.         CameraEffectComponents[26] = transform.Find("GO Root/Camera/Test19(Clone)/Poss/Camera").gameObject;
    70.         CameraEffectComponents[27] = transform.Find("GO Root/Camera/Test20(Clone)/Poss/Camera").gameObject;
    71.         CameraEffectComponents[28] = transform.Find("GO Root/Camera/Test21(Clone)/Poss/Camera").gameObject;
    72.         CameraEffectComponents[29] = transform.Find("GO Root/Camera/Test22(Clone)/Poss/Camera").gameObject;
    73.         CameraEffectComponents[30] = transform.Find("GO Root/Camera/Test23(Clone)/Poss/Camera").gameObject;
    74.         CameraEffectComponents[31] = transform.Find("GO Root/Camera/Test24(Clone)/Poss/Camera").gameObject;
    75.         CameraEffectComponents[32] = transform.Find("GO Root/Camera/Test25(Clone)/Poss/Camera").gameObject;
    76.         CameraEffectComponents[33] = transform.Find("GO Root/Camera/Test26(Clone)/Poss/Camera").gameObject;
    77.         CameraEffectComponents[34] = transform.Find("GO Root/Camera/Test27(Clone)/Poss/Camera").gameObject;
    78.         CameraEffectComponents[34] = transform.Find("GO Root/Camera/Test28(Clone)/Poss/Camera").gameObject;
    79.         CameraEffectComponents[35] = transform.Find("GO Root/Camera/Test29(Clone)/Poss/Camera").gameObject;
    80.         CameraEffectComponents[36] = transform.Find("GO Root/Camera/HORSE_JOCKEY(Clone)").gameObject;
    81.         CameraEffectComponents[37] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_1(Clone)/Pos/Camera").gameObject;
    82.         CameraEffectComponents[38] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_2(Clone)/Pos/Camera").gameObject;
    83.         CameraEffectComponents[39] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_3(Clone)/Pos/Camera").gameObject;
    84.         CameraEffectComponents[40] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/FRONT_1(Clone)/Pos/Camera").gameObject;
    85.         CameraEffectComponents[41] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_1(Clone)/Pos/Camera").gameObject;
    86.         CameraEffectComponents[42] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_2(Clone)/Pos/Camera").gameObject;
    87.         //lets use a lambada expression here for declaring a null
    88.         if (CameraEffectComponents.Any(e => e != null))
    89.         {
    90.             foundAllComponents = true;
    91.         } else
    92.         {
    93.             foundAllComponents = false;
    94.         }
    95.         yield return null;
    96.     }
    97.    
    98.     void GetFogs()
    99.     {
    100.         int env_idx = tzPlayInfo.Instance.ENV_01_IDX;
    101.         //0: day, 1: night
    102.         if (env_idx == 0 && FogEffect == null)
    103.         {
    104.             FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    105.         } else if (env_idx == 1 && FogEffect == null)
    106.         {
    107.             FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    108.         } else
    109.         {
    110.             Debug.Log("There's something wrong!");
    111.         }
    112.     }
    113.     void OptionCameraOnAndOff()
    114.     {
    115.         foreach (GameObject cam in CameraEffectComponents)
    116.         {
    117.             if (PlayerPrefs.GetInt(OptionPopup.CameraOptionSaving) == 1)
    118.             {
    119.                 if (cam.GetComponent<Bloom>() != null)
    120.                 {
    121.                     cam.GetComponent<Bloom>().enabled = true;
    122.                 }
    123.                 if (cam.GetComponent<DepthOfFieldDeprecated>() != null)
    124.                 {
    125.                     cam.GetComponent<DepthOfFieldDeprecated>().enabled = true;
    126.                 }
    127.                 if (cam.GetComponent<DepthOfField>() != null)
    128.                 {
    129.                     cam.GetComponent<DepthOfField>().enabled = true;
    130.                 }
    131.             }
    132.             else
    133.             {
    134.                 if (cam.GetComponent<Bloom>() != null)
    135.                 {
    136.                     cam.GetComponent<Bloom>().enabled = false;
    137.                 }
    138.                 if (cam.GetComponent<DepthOfFieldDeprecated>() != null)
    139.                 {
    140.                     cam.GetComponent<DepthOfFieldDeprecated>().enabled = false;
    141.                 }
    142.                 if (cam.GetComponent<DepthOfField>() != null)
    143.                 {
    144.                     cam.GetComponent<DepthOfField>().enabled = false;
    145.                 }
    146.             }
    147.         }
    148.     }
    149.     void OptionFogOnAndOff()
    150.     {
    151.         if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
    152.         {
    153.             FogEffect.SetActive(true);
    154.         }
    155.         else
    156.         {
    157.             FogEffect.SetActive(false);
    158.         }
    159.     }
    160. }
    161.  
    is this more better or not and now i have this error

    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.
     
    Last edited: Feb 22, 2018
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Then you can turn it off or on instead of constantly doing so in an update. Just do it once when they select to turn it on or off. Otherwise, it's always running.
     
  14. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I have changed my code . Would you mind if you take a look at it ?
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Sure, can take a look at it.
     
  16. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can surely do this in a different way. If you hard-code it, there will be a more flexible and less error-prone approach.

    I'm not entirely sure how your scene looks like, but it's definitely true. Using that many transform.Find calls regularly may take a significant influence on performance, just as well as all the calls to GetComponent, hence you should avoid it.

    Instead, you should try to encapsulate the logic in a seperate component. If behaviour differs, subclass it (or use interfaces), if the values differ, expose them to the inspector and build multiple prefabs.
    You can then evaluate the setups once (optionally at editor time but more importantly at runtime in Awake) and operate on these components without all the overhead.
     
  17. Ginxx009

    Ginxx009

    Joined:
    Sep 11, 2016
    Posts:
    89
    I reconstructed my code sir could you please take a look at it