统一的 StackOverflowException 函数 gameobject.transform.childcount

StackOverflowException Function in unity gameobject.transform.childcount

Unity错误显示

堆栈溢出异常 CheckWord.GettingLength(UnityEngine.GameObject obj)(位于 Assets/Scripts/Player/CheckWord.cs:906)

函数正在获取游戏对象的内部子节点数:-

   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;

public class CheckWord : MonoBehaviour {

    public GameObject DropZone;
    public GameObject player;
    public GameObject KiloMeter;
    public GameObject OKiloMeter;
    public GameObject WinStatus;
    public GameObject TournamentWin;
    public GameObject KeyBoardLock;

    public Transform camera_main;

    [Header("CameraRelated")]
    public float power =1.7f;
    public float duration=0.5f;
    public float slowDownAmount=0.5f;
    public bool shouldShake = false;





    Vector3 startposition;
    float initalDuration;


    public TextAsset WordList;

    [HideInInspector]
    public string[] txtlist;

    GameObject[] AllChild;
    string MakeWord="";
    int WordValue;

    Animator anim;
    Rigidbody2D body;



    string LevelName="";

    [HideInInspector]
    public int ValueA = 0;
    [HideInInspector]
    public int ValueB = 0;
    [HideInInspector]
    public int ValueC = 0;
    [HideInInspector]
    public int ValueD = 0;
    [HideInInspector]
    public int ValueE = 0;
    [HideInInspector]
    public int ValueF = 0;
    [HideInInspector]
    public int ValueG = 0;
    [HideInInspector]
    public int ValueH = 0;
    [HideInInspector]
    public int ValueI = 0;
    [HideInInspector]
    public int ValueJ = 0;
    [HideInInspector]
    public int ValueK = 0;
    [HideInInspector]
    public int ValueL = 0;
    [HideInInspector]
    public int ValueM = 0;
    [HideInInspector]
    public int ValueN = 0;
    [HideInInspector]
    public int ValueO = 0;
    [HideInInspector]
    public int ValueP = 0;
    [HideInInspector]
    public int ValueQ = 0;
    [HideInInspector]
    public int ValueR = 0;
    [HideInInspector]
    public int ValueS = 0;
    [HideInInspector]
    public int ValueT = 0;
    [HideInInspector]
    public int ValueU = 0;
    [HideInInspector]
    public int ValueV = 0;
    [HideInInspector]
    public int ValueW = 0;
    [HideInInspector]
    public int ValueX = 0;
    [HideInInspector]
    public int ValueY = 0;
    [HideInInspector]
    public int ValueZ = 0;



    [Header("KeyBoard")]
    public GameObject Letter_A;
    public GameObject Letter_B;
    public GameObject Letter_C;
    public GameObject Letter_D;
    public GameObject Letter_E;
    public GameObject Letter_F;
    public GameObject Letter_G;
    public GameObject Letter_H;
    public GameObject Letter_I;
    public GameObject Letter_J;
    public GameObject Letter_K;
    public GameObject Letter_L;
    public GameObject Letter_M;
    public GameObject Letter_N;
    public GameObject Letter_O;
    public GameObject Letter_P;
    public GameObject Letter_Q;
    public GameObject Letter_R;
    public GameObject Letter_S;
    public GameObject Letter_T;
    public GameObject Letter_U;
    public GameObject Letter_V;
    public GameObject Letter_W;
    public GameObject Letter_X;
    public GameObject Letter_Y;
    public GameObject Letter_Z;


    [Header("Panel")]
    public GameObject UpperPanel;
    public GameObject MiddlePanel;
    public GameObject Middle1Panel;
    public GameObject BottomPanel;


    string KiloM="";
    int TournamentScene;
    int objCount;






    public void Start()
    {
        LevelName = PlayerPrefs.GetString ("buttonName","Sprint");
        GenrateWord ();
        KiloMeter.GetComponent<Text> ().text ="120";
        OKiloMeter.GetComponent<Text> ().text ="120";
        KiloM=KiloMeter.GetComponent<Text> ().text ;
        TournamentScene = PlayerPrefs.GetInt ("Tournament", 0);
        InitWord ();
        WordValue = 0;
        camera_main = Camera.main.transform;
        startposition = camera_main.localPosition;
        initalDuration = duration;

        StartCoroutine (wordraw());
    }

    IEnumerator wordraw()
    {
        for (int i = 0; i < 26; i++) {
            CreateWords ();
            yield return new WaitForSeconds (0.001f);
        }
        System.GC.Collect ();
    }

    public void InitWord()
    {
        switch (LevelName) 
        {
        case "SPRINT":

            ValueA = 1;
            Letter_A.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_A");

            ValueB= 3;
            Letter_B.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_B");

            ValueC = 3;
            Letter_C.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_C");

            ValueD = 2;
            Letter_D.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_D");

            ValueE = 1;
            Letter_E.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_E");

            ValueF = 4;
            Letter_F.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_F");

            ValueG = 2;
            Letter_G.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_G");

            ValueH = 4;
            Letter_H.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_H");

            ValueI = 1;
            Letter_I.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_I");

            ValueJ = 8;
            Letter_J.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_J");

            ValueK = 5;
            Letter_K.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_K");

            ValueL = 1;
            Letter_L.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_L");

            ValueM = 3;
            Letter_M.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_M");

            ValueN = 1;
            Letter_N.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_N");

            ValueO = 1;
            Letter_O.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_O");

            ValueP = 3;
            Letter_P.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_P");

            ValueQ = 10;
            Letter_Q.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_Q");

            ValueR = 1;
            Letter_R.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_R");

            ValueS = 1;
            Letter_S.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_S");

            ValueT = 1;
            Letter_T.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_T");

            ValueU = 1;
            Letter_U.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_U");

            ValueV = 4;
            Letter_V.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_v");

            ValueW = 4;
            Letter_W.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_W");

            ValueX = 8;
            Letter_X.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_X");

            ValueY = 4;
            Letter_Y.GetComponent<Image>().sprite=Resources.Load<Sprite>("Images/MainGame/Letters With Values/Sprint Mode/Eng_Y");

            ValueZ = 10;
            Letter_Z.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_Z");

            break;

        case "TIME":

            ValueA = 1;
            Letter_A.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_A");

            ValueB= 1;
            Letter_B.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_B");

            ValueC = 1;
            Letter_C.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_C");

            ValueD = 1;
            Letter_D.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_D");

            ValueE = 1;
            Letter_E.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_E");

            ValueF = 1;
            Letter_F.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_F");

            ValueG = 1;
            Letter_G.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_G");

            ValueH = 1;
            Letter_H.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_H");

            ValueI = 1;
            Letter_I.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_I");

            ValueJ = 1;
            Letter_J.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_J");

            ValueK = 1;
            Letter_K.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_K");

            ValueL = 1;
            Letter_L.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_L");

            ValueM = 1;
            Letter_M.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_M");

            ValueN = 1;
            Letter_N.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_N");

            ValueO = 1;
            Letter_O.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_O");

            ValueP = 1;
            Letter_P.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_P");

            ValueQ = 1;
            Letter_Q.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_Q");

            ValueR = 1;
            Letter_R.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_R");
            ValueS = 1;

            Letter_S.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_S");

            ValueT = 1;
            Letter_T.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_T");

            ValueU = 1;
            Letter_U.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_U");

            ValueV = 1;
            Letter_V.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_V");

            ValueW = 1;
            Letter_W.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_W");

            ValueX = 1;
            Letter_X.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_X");

            ValueY = 1;
            Letter_Y.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_Y");

            ValueZ = 1;
            Letter_Z.GetComponent<Image>().sprite=Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Time Mode/Eng_Z");

            break;

        case "BLACKOUT":

            ValueA = 1;
            Letter_A.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_A");

            ValueB = 3;
            Letter_B.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_B");

            ValueC = 3;
            Letter_C.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_C");

            ValueD = 2;
            Letter_D.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_D");

            ValueE = 1;
            Letter_E.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_E");

            ValueF = 4;
            Letter_F.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_F");

            ValueG = 2;
            Letter_G.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_G");

            ValueH = 4;
            Letter_H.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_H");

            ValueI = 1;
            Letter_I.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_I");

            ValueJ = 8;
            Letter_J.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_J");

            ValueK = 5;
            Letter_K.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_K");

            ValueL = 1;
            Letter_L.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_L");

            ValueM = 3;
            Letter_M.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_M");

            ValueN = 1;
            Letter_N.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_N");

            ValueO = 1;
            Letter_O.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_O");

            ValueP = 3;
            Letter_P.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_P");

            ValueQ = 10;
            Letter_Q.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_Q");

            ValueR = 1;
            Letter_R.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_R");

            ValueS = 1;
            Letter_S.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_S");

            ValueT = 1;
            Letter_T.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_T");

            ValueU = 1;
            Letter_U.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_U");

            ValueV = 4;
            Letter_V.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_v");

            ValueW = 4;
            Letter_W.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_W");

            ValueX = 8;
            Letter_X.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_X");

            ValueY = 4;
            Letter_Y.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_Y");

            ValueZ = 10;
            Letter_Z.GetComponent<Image> ().sprite = Resources.Load<Sprite> ("Images/MainGame/Letters With Values/Sprint Mode/Eng_Z");

            break;
        }

    }


    public void GenrateWord()
    {
        WordList = (TextAsset)Resources.Load ("word list/final_list");
    }



    public void CheckSpell()
    {
        MakeWord = "";

        int tlChild=GettingLength(DropZone); 


        WordValue = 0;
        Debug.Log ("Total Child Lenght" + tlChild.ToString());

        anim=player.GetComponent<Animator> ();
        body=player.GetComponent<Rigidbody2D> ();
        AllChild=new GameObject[tlChild];

        for (int i = 0; i < tlChild; i++) {
            AllChild[i]=DropZone.transform.GetChild(i).gameObject;
            string ObjectName=DropZone.transform.GetChild (i).gameObject.name.ToString ();
            MakeWord = MakeWord + DropZone.transform.GetChild (i).gameObject.name;
            WordValue = WordValue + ValuesOfWord (ObjectName);
        }

        int z=AllChild.Length;

        Debug.Log ("All Child Lenght" + z.ToString());
        Debug.Log ("MakeWord="+MakeWord);
        if(z>=2){
            if (WordCheck ()) {
                anim.SetInteger ("State", 1);
                Debug.Log ("WordCheck Enter Word is" + MakeWord);
                Debug.Log ("WordCheck Enter WordValue is" + WordValue.ToString ());
                int dis = int.Parse (KiloM);

                for (int i = 0; i < tlChild; i++) {
                    CreateWords ();
                    Destroy (DropZone.transform.GetChild (i).gameObject);

                }



                if (dis >= 0) {
                    dis = dis - WordValue;  
                    if (dis >= 0) {
                        KiloM = dis.ToString ();
                        KiloMeter.GetComponent<Text> ().text = KiloM;
                    } else {
                        if (TournamentScene == 1) {
                            TournamentWin.SetActive (true);
                        } else {
                            WinStatus.SetActive (true);
                        }
                    }
                } else {
                    if (TournamentScene == 1) {
                        TournamentWin.SetActive (true);
                    } else {
                        WinStatus.SetActive (true);
                    }
                }

                for (int i = 0; i <= 1 * WordValue; i++) {
                    StartCoroutine (Runner (10 * WordValue));
                }
            } else {
                shouldShake = true;
            }
        } else {
            shouldShake = true;
        }
    }

    void Update()
    {
        if (shouldShake) {
            if (duration > 0) {
                camera_main.localPosition = camera_main.position + UnityEngine.Random.insideUnitSphere * power;
                duration -= Time.deltaTime * slowDownAmount;
            } else {
                shouldShake = false;
                duration = initalDuration;
                camera_main.localPosition = startposition;
            }
        }
    }


    public bool WordCheck ()
    {
        bool flag = false;

        if (WordList.text.Contains (MakeWord)) {
            flag=true;
        } else {
            flag = false;
        }
        Debug.Log ("Flag="+flag);
        return flag;
    }

    public int ValuesOfWord(string word)
    {
        int SendValue = 0;
        switch (word)
        {
        case "A":
            SendValue = ValueA;
            break;

        case "B":
            SendValue = ValueB;
            break;

        case "C":
            SendValue = ValueC;
            break;

        case "D":
            SendValue = ValueD;
            break;

        case "E":
            SendValue = ValueE;
            break;

        case "F":
            SendValue = ValueF;
            break;

        case "G":
            SendValue = ValueG;
            break;

        case "H":
            SendValue = ValueH;
            break;

        case "I":
            SendValue = ValueI;
            break;

        case "J":
            SendValue = ValueJ;
            break;

        case "K":
            SendValue = ValueK;
            break;

        case "L":
            SendValue = ValueL;
            break;

        case "M":
            SendValue =ValueM;
            break;

        case "N":
            SendValue =ValueN;
            break;

        case "O":
            SendValue =ValueO;
            break;

        case "P":
            SendValue =ValueP;
            break;

        case "Q":
            SendValue =ValueQ;
            break;

        case "R":
            SendValue =ValueR;
            break;

        case "S":
            SendValue = ValueS;
            break;

        case "T":
            SendValue = ValueT;
            break;

        case "U":
            SendValue =ValueU;
            break;

        case "V":
            SendValue =ValueV;
            break;

        case "W":
            SendValue =ValueW;
            break;

        case "X":
            SendValue =ValueX;
            break;

        case "Y":
            SendValue =ValueY;
            break;

        case "Z":
            SendValue = ValueZ;
            break;

        }
        Debug.Log ("SendValue = "+SendValue);
        return SendValue;
    }




    IEnumerator Runner(int n)
    {

        for(int i=0;i<n;i++)
        {
            anim.SetInteger ("State", 1);
            body.AddForce(new Vector2(500f,0));
            yield return new WaitForSeconds (0.05f);
        }
        anim.SetInteger ("State", 0);
    }

    public void CreateWords()
    {

        int j = randomValueGenrate(0, 25);
        int n = randomValueGenrate(0, 4);
        switch (n) {

        case 0:
            objCount = 0;
            objCount = GettingLength (UpperPanel);

            if (objCount <= 6) {
                GameObject obj=Instantiate (WordsGet (j), UpperPanel.transform) as GameObject;
                obj.name=obj.name.Replace("(Clone)", "");

                if (moreSameObject (obj)) {
                    Destroy (obj);
                    CreateWords ();
                }

            } else {
                CreateWords ();
            }
            break;
        case 1:
            objCount = 0;
            objCount = GettingLength (Middle1Panel);
            if (objCount <= 6) {
                GameObject obj=Instantiate (WordsGet (j), Middle1Panel.transform) as GameObject;
                obj.name=obj.name.Replace("(Clone)", "");

                if (moreSameObject (obj)) {
                    Destroy (obj);
                    CreateWords ();
                }

            } else {
                CreateWords ();
            }

            break;
        case 2:
            objCount=0;
            objCount = GettingLength (MiddlePanel);

            if (objCount <= 6) {
                GameObject obj=Instantiate (WordsGet (j), MiddlePanel.transform) as GameObject;
                obj.name=obj.name.Replace("(Clone)", "");

                if (moreSameObject (obj)) {
                    Destroy (obj);
                    CreateWords ();
                }

            } else {
                CreateWords ();
            }

            break;

        case 3:
            objCount=0;
            objCount = GettingLength (BottomPanel);

            if (objCount <= 6) {
                GameObject obj=Instantiate (WordsGet (j), BottomPanel.transform) as GameObject;
                obj.name=obj.name.Replace("(Clone)", "");

                if (moreSameObject (obj)) {
                    Destroy (obj);
                    CreateWords ();
                }

            } else {
                CreateWords ();
            }
            break;
        }
    }

    public bool moreSameObject(GameObject LObject)
    {
        int count = 0;
        bool flag = false;

        int UpperSize=0;
        int MiddleSize=0;
        int Middle1Size=0;
        int BottomSize=0;

        UpperSize = GettingLength (UpperPanel);
        MiddleSize = GettingLength (MiddlePanel);
        Middle1Size = GettingLength (Middle1Panel);
        BottomSize = GettingLength (BottomPanel);


        for (int i = 0; i < UpperSize; i++) {
            if (UpperPanel.transform.GetChild (i).name.Equals (LObject.name)) {
                count++;
            }
        }
        for (int i = 0; i < MiddleSize; i++) {
            if (MiddlePanel.transform.GetChild (i).name.Equals (LObject.name)) {
                count++;
            }
        }
        for (int i = 0; i < Middle1Size; i++) {
            if (Middle1Panel.transform.GetChild (i).name.Equals (LObject.name)) {
                count++;
            }
        }
        for (int i = 0; i < BottomSize; i++) {
            if (BottomPanel.transform.GetChild (i).name.Equals (LObject.name)) {
                count++;
            }
        }

        if (count > 2) {
            Debug.Log ("Found More then 2"+LObject.name);
            flag = true;
        }

        return flag;
    }

    public GameObject WordsGet(int j)
    {
        GameObject LetterObject=null;

        switch(j)
        {
        case 0:
            LetterObject = Letter_A;
            break;
        case 1:
            LetterObject = Letter_B;
            break;
        case 2:
            LetterObject = Letter_C;
            break;
        case 3:
            LetterObject = Letter_D;
            break;
        case 4:
            LetterObject = Letter_E;
            break;
        case 5:
            LetterObject = Letter_F;
            break;
        case 6:
            LetterObject = Letter_G;
            break;
        case 7:
            LetterObject = Letter_H;
            break;
        case 8:
            LetterObject = Letter_I;
            break;
        case 9:
            LetterObject = Letter_J;
            break;
        case 10:
            LetterObject = Letter_K;
            break;
        case 11:
            LetterObject = Letter_L;
            break;

        case 12:
            LetterObject = Letter_M;
            break;
        case 13:
            LetterObject = Letter_N;
            break;
        case 14:
            LetterObject = Letter_O;
            break;
        case 15:
            LetterObject = Letter_P;
            break;
        case 16:
            LetterObject = Letter_Q;
            break;
        case 17:
            LetterObject = Letter_R;
            break;
        case 18:
            LetterObject = Letter_S;
            break;
        case 19:
            LetterObject = Letter_T;
            break;
        case 20:
            LetterObject = Letter_U;
            break;
        case 21:
            LetterObject = Letter_V;
            break;
        case 22:
            LetterObject = Letter_W;
            break;
        case 23:
            LetterObject = Letter_X;
            break;
        case 24:
            LetterObject = Letter_Y;
            break;
        case 25:
            LetterObject = Letter_Z;
            break;
        }
        Debug.Log ("Letter Name"+LetterObject.name.ToString());
        return LetterObject;
    }


    public int randomValueGenrate(int min,int max)
    {
        System.Random random = new System.Random ();

        int r = random.Next (max-min)+min;

        if (r > max) {
            randomValueGenrate (min, max);
        } else {            
            return r;
        }

        return r;
    }


    public int GettingLength(GameObject obj)
    {
        if (null == obj)
        {
            return 0;
            Debug.Log ("Getting Object is Null");
        }

        int count = 0;

        foreach (Transform child in obj.transform)
        {
            if (null != child) {
                count += 1;

            } else {
                continue;
                Debug.Log ("Getting Chils is N");
            }

        }

        return count;
    }

}

错误显示这一行 (foreach (Transform child in obj.transform))

在此功能之前,我使用 Gameobject.transform.childcount 它也显示相同的错误

我在任何地方都找不到正确的答案 如果有人知道如何解决,请给我答案

将脚本中的 Destroy() 替换为 DestroyImmediate()

这里有详细的解释:
如果您使用 Destroy() 函数销毁 GameObject,Unity 不会立即销毁它。它实际上所做的只是将其标记为 null 并在以后的帧中销毁它。所以如果你在一帧中销毁一个 GameObject ,并在完全相同的帧中查询子计数,你得到的数字不会减少。如果你用 DestroyImmediate() 来做,函数签名中标记的 GameObject 将立即被销毁,子计数将立即递减。
在您的情况下,如果已经有另一个与您创建的字母相同的字母,则 CreateWords() 函数会销毁创建的字母对象,然后再次调用该函数本身。然而,即使字母对象被销毁,子计数仍然增加 1。最后,函数会发现每个面板都有超过 6 个字母(实际上没有),导致无限递归循环导致崩溃程序。