0
\$\begingroup\$

In the Unity Editor, this code helps me to get the Fog GameObject:

//get the fog GameObject
//0 :day , 1:night
if (tzPlayInfo.Instance.ENV_01_IDX == 0)
{
      if (FogEffect == null)
      {
             FogEffect = this.transform.GetChild(0).GetChild(2).GetChild(0).GetChild(4).gameObject;
      }

} else
{
      if (FogEffect == null)
      {
             FogEffect = this.transform.GetChild(0).GetChild(2).GetChild(1).GetChild(5).gameObject;
      }
}

And here's what in my hierarchy (for the night case):

enter image description here

enter image description here

As you can see on the photo it successfully gets the Fog GameObject. But when i build it and run the apk it gives me

UnityException: Transform child out of bounds at MC_SettingBehaviour.Update () [0x00000] in :0


I tried what Arcane Engineer told me to do by using Transform.Find()

if (tzPlayInfo.Instance.ENV_01_IDX == 0)
{
      FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;

} else{
      FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
}

But the problem when I build & run an APK, the error I get is replaced by:

UnassignedReferenceException: The variable FogEffect of MC_SettingBehaviour has not been assigned

This is how i load my fog env

// 4. fog env.
        data.GetFogEnv(ref env03_idx, out prefabs);
        Load(ref prefabs, ref weather_env_root);

and I created a Load function here it is

void Load(ref GameObject[] prefabs, ref Transform root)
{
    if (prefabs != null)
    {
        GameObject obj;
        for (int i = 0; i < prefabs.Length; i++)
        {
            if (prefabs[i] == null) continue;
            obj = Instantiate(prefabs[i], root);
            obj.name = prefabs[i].name;
        }
    }
}

And here is my GetFogEnv

public void GetFogEnv(ref int env03_idx, out GameObject[] prefabs)
{
    prefabs = (env03_idx == 2 || env03_idx == 3) ? fog : null;
}

EDITTED:

[SerializeField] GameObject[] cameraGameObject = new GameObject[46];

[SerializeField] GameObject FogEffect;

bool foundobjects = false;
void Update()
{
    if (tzPlayInfo.Instance.BLOCK_GAME_FLOW == false)
    {
        //if the option is livestreaming lets disable this script because it's useless.
        if (PlayerPrefs.GetInt(OptionPopup.LiveStreaming_) == 1)
        {
            this.enabled = false;
        }
        else
        {
            //Find Objects
            if (!foundobjects)
            {
                StartCoroutine(FindObjects());
            } else
            {
                StopCoroutine(FindObjects());
            }
        }
        //if it is all found then execute this statement
        if (foundobjects)
        {
            //Save of Camera
            if (PlayerPrefs.GetInt(OptionPopup.CameraOptionSaving) == 1)
            {
                foreach (GameObject cameras in cameraGameObject)
                {
                    //lets check if theres a component
                    if (cameras.GetComponent<Bloom>() == null)
                    {

                    } else
                    {
                        cameras.GetComponent<Bloom>().enabled = true;
                    }

                    if (cameras.GetComponent<DepthOfField>() == null)
                    {

                    } else
                    {
                        cameras.GetComponent<DepthOfField>().enabled = true;
                    }

                    if (cameras.GetComponent<DepthOfFieldDeprecated>() == null)
                    {

                    } else
                    {
                        cameras.GetComponent<DepthOfFieldDeprecated>().enabled = true;
                    }
                }
            }
            else
            {
                foreach (GameObject cameras in cameraGameObject)
                {
                    //lets check if theres a component
                    if (cameras.GetComponent<Bloom>() == null)
                    {

                    }
                    else
                    {
                        cameras.GetComponent<Bloom>().enabled = false;
                    }

                    if (cameras.GetComponent<DepthOfField>() == null)
                    {

                    }
                    else
                    {
                        cameras.GetComponent<DepthOfField>().enabled = false;
                    }

                    if (cameras.GetComponent<DepthOfFieldDeprecated>() == null)
                    {

                    }
                    else
                    {
                        cameras.GetComponent<DepthOfFieldDeprecated>().enabled = false;
                    }
                }
            }
            //Start of Fog
            if (PlayerPrefs.GetInt(OptionPopup.FogOptionSaving) == 1)
            {
                FogEffect.SetActive(true);
            }
            else
            {
                FogEffect.SetActive(false);
            }
        }
    }
}

IEnumerator FindObjects()
{
    //AIR 
    cameraGameObject[0] = transform.Find("GO Root/Camera/AIR_1(Clone)/GameObject/Camera").gameObject;
    cameraGameObject[1] = transform.Find("GO Root/Camera/AIR_2(Clone)/Camera").gameObject;

    //Paddock
    cameraGameObject[2] = transform.Find("GO Root/Camera/Paddock_1(Clone)/CameraPos/GameObject/Camera").gameObject;

    //TV
    cameraGameObject[3] = transform.Find("GO Root/Camera/TV_1_far(Clone)/Poss/Camera").gameObject;
    cameraGameObject[4] = transform.Find("GO Root/Camera/TV_1_lead(Clone)/Poss/Camera").gameObject;
    cameraGameObject[5] = transform.Find("GO Root/Camera/TV_2_far(Clone)/Poss/Camera").gameObject;
    cameraGameObject[6] = transform.Find("GO Root/Camera/TV_2_lead(Clone)/Poss/Camera").gameObject;

    //LAST_SPURT && Test 30
    cameraGameObject[7] = transform.Find("GO Root/Camera/LAST_SPURT_1(Clone)/Pos/Camera").gameObject;
    cameraGameObject[8] = transform.Find("GO Root/Camera/Test30(Clone)/Poss/Camera").gameObject;

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

    //CommonEnv (follow_camera's_focus)
    cameraGameObject[40] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_1(Clone)/Pos/Camera").gameObject;
    cameraGameObject[41] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_2(Clone)/Pos/Camera").gameObject;
    cameraGameObject[42] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/TOP_3(Clone)/Pos/Camera").gameObject;
    cameraGameObject[43] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/FRONT_1(Clone)/Pos/Camera").gameObject;
    cameraGameObject[44] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_1(Clone)/Pos/Camera").gameObject;
    cameraGameObject[45] = transform.Find("GO Root/CommonEnv/follow_camera's_focus/SIDE_2(Clone)/Pos/Camera").gameObject;

    //get the fog GameObject
    //0 :day , 1:night
    if (tzPlayInfo.Instance.ENV_01_IDX == 0)
    {
        //Find GameObject Fog Effect
        FogEffect = transform.Find("GO Root/day/01_Env_Lev01_test01/Fog01 (1)").gameObject;
    }
    else
    {
        //Find GameObject Fog Effect
        FogEffect = transform.Find("GO Root/night/01_Env_Lev01_night-test02/Fog01").gameObject;
    }

    foreach (GameObject cams in cameraGameObject)
    {
        if (cams != null && FogEffect != null)
        {
            foundobjects = true;
            Debug.Log("All of the GameObjects have been found!!!!");
        }
    }
    yield break;
}

I build an apk using this code now it has this error

02-14 13:14:47.306 1828-1958/system_process W/InputDispatcher: channel '4ecb0710 com.x.x.x/com.unity3d.player.UnityPlayerActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9

02-14 13:14:47.306 1828-1958/system_process E/InputDispatcher: channel '4ecb0710 com.steet383.rh.google/com.unity3d.player.UnityPlayerActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

02-14 13:14:47.306 1828-1846/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '4ecb0710 com.x.x.x/com.unity3d.player.UnityPlayerActivity (server)'

02-14 13:14:47.306 1828-1846/system_process I/WindowState: WIN DEATH: Window{4ecb0710 u0 com.x.x.x/com.unity3d.player.UnityPlayerActivity}

02-14 13:14:47.306 1828-1846/system_process W/WindowManager: Force-removing child win Window{4ebbdfa0 u0 SurfaceView} from container Window{4ecb0710 u0 com.x.x.x/com.unity3d.player.UnityPlayerActivity}

02-14 13:14:47.306 1828-2020/system_process W/ActivityManager: Force removing ActivityRecord{4ea0bc74 u0 com.x.x.x.UnityPlayerActivity t8}: app died, no saved state

\$\endgroup\$
  • \$\begingroup\$ Hi NoobProgrammer. Firstly, I know you're trying to be respectful by using the word "sir," but Arcane Engineer has asked you to please not call them that. Just using a person's username is sufficient. Secondly, can you tell us where this fog object comes from? Does it get spawned dynamically at runtime or is it part of the scene in advance? Does it have any particular Components attached to it that we could look for? \$\endgroup\$ – DMGregory Feb 13 '18 at 12:39
  • \$\begingroup\$ @DMGregory it is spawned during run time . So what i did there is this if (tzPlayInfo.Instance.BLOCK_GAME_FLOW == false) { this line of code is the trigger the all my resources are loaded now and ready to play the game . \$\endgroup\$ – NoobProgrammer Feb 14 '18 at 1:25
  • \$\begingroup\$ Can you show the script that spawns the fog? We might be able to use that to pass the fog reference to where it needs to be, if searching is proving difficult. \$\endgroup\$ – DMGregory Feb 14 '18 at 1:26
  • \$\begingroup\$ I editted my question @DMGregory \$\endgroup\$ – NoobProgrammer Feb 14 '18 at 1:31
1
\$\begingroup\$

Use Transform.Find(string name) instead. Or keep public references that you can populate via inspector, e.g.

public Transform myLightSource;

You should never, EVER be using magic numbers in your code to retrieve various elements as that inevitably leads to the sort of problems you are having right now. Always use string names/IDs or symbolic references e.g. a constant like MY_LIGHT_SOURCE where that would be defined as const int MY_LIGHT_SOURCE = 7; or #define MY_LIGHT_SOURCE 7.

Moreover, in your specific case, Transform order is not guaranteed at runtime, so you are requesting an invalid index when running as APK. This will not happen if you Find by name.

\$\endgroup\$
  • \$\begingroup\$ How can i do it in Transform.Find() sir? Something like this : FogEffect = Transform.Find("Go Root/night/01_Env_Lev01_night-test02/Fog01 (1)"); \$\endgroup\$ – NoobProgrammer Feb 13 '18 at 6:09
  • \$\begingroup\$ @NoobProgrammer From the docs I linked: //Find the child named "ammo" of the gameobject "magazine" (magazine is a child of "gun"). ammo = gun.transform.Find("magazine/ammo"); \$\endgroup\$ – Engineer Feb 13 '18 at 6:12
  • \$\begingroup\$ FogEffect.transform.Find("Go Root/night/01_Env_Lev01_night-test02/Fog01 (1)"); so like that sir? \$\endgroup\$ – NoobProgrammer Feb 13 '18 at 6:12
  • \$\begingroup\$ @NoobProgrammer I don't know your structure, and I don't care about the specific details. Figure it out yourself - try it, test it. You have the reference to the documentation. That's how the rest of us do it. \$\endgroup\$ – Engineer Feb 13 '18 at 6:12
  • 2
    \$\begingroup\$ @NoobProgrammer Here's a troubleshooting strategy you can use to find where your implementation is going wrong: try each level of the find individually. So var root = transform.Find("GO Root"); if(root == null) { /* log your error & exit */ } var day = root.Find("day"); if(day == null) { /* log a different error & exit */ } ... on and on down each level of the hierarchy. Then when you run, the error will tell you exactly where in the long hierarchy chain something undesired/unexpected happened, helping you focus your debugging efforts there. \$\endgroup\$ – DMGregory Feb 13 '18 at 12:52

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.