缺少参考无法理解原因

时间:2019-05-14 02:30:13

标签: reference

我的问题是,一旦销毁并重新加载对象,我的对象就无法检测到它们。我正在使用从块的相邻位置进行射线投射。加载全新的块时,怎么可能会丢失引用?

我已经查看了脚本,但看不到问题所在。当然,这与删除块有关,并且与光线投射有关的某种问题或其他问题,但是这是怎么发生的?

 void LoadMap()
    {
        ClearMap();

        Debug.LogWarning("loading");

        string dataAsJson = File.ReadAllText(Application.dataPath + "/Resources/Saves/save.txt");

        TileData tileData = JsonUtility.FromJson<TileData>(dataAsJson);



        int foundObjects = 0;
        Debug.LogWarning("map " + tileData);
        for(int i = 0; i < tileData.blockData.Count;i++)
        {
            foundObjects++;
            //Debug.LogWarning("loaded " + tileData.blockData[i].blockName);
        }
        Debug.LogWarning("found " + foundObjects + " objects");




        InstantiateLoadedDataIntoBlocks(tileData);

        UpdateBlock[] allBlocks = FindObjectsOfType<UpdateBlock>();

        for(int i = 0; i < allBlocks.Length; i++)
        {
            updateBlock = allBlocks[i].GetComponent<UpdateBlock>();
            updateBlock.DetectSurroundings(allBlocks[i].transform.position);
            updateBlock.SurroundingsDetectBlock(allBlocks[i].gameObject);

            GameObject[] blocksOnTile = updateBlock.GetBlocksOnThisTile(allBlocks[i].transform.position);

            GameObject blockUnderneath = updateBlock.GetBlockUnderneath(blocksOnTile);
        }


        disconnectAll.DisconnectAllCells();
        reconnectAll.Reconnect();
    }
 public void DetectSurroundings(Vector3 objectLocation)
    {
        north = objectLocation + new Vector3(0, 1f, 0);
        south = objectLocation + new Vector3(0, -1f, 0);
        east = objectLocation + new Vector3(1f, 0, 0);
        west = objectLocation + new Vector3(-1f, 0, 0);



        northObject = getObjectID.RayDetectFloor(north);
        Debug.LogWarning("debug: north object is " + northObject);
        northTopObject = getObjectID.RayDetectTop(north);

        southObject = getObjectID.RayDetectFloor(south);
        Debug.LogWarning("debug: south object is " + southObject);
        southTopObject = getObjectID.RayDetectTop(south);


        eastObject = getObjectID.RayDetectFloor(east);
        Debug.LogWarning("debug: east object is " + eastObject);
        eastTopObject = getObjectID.RayDetectTop(east);

        westObject = getObjectID.RayDetectFloor(west);
        Debug.LogWarning("debug: west object is " + westObject);
        westTopObject = getObjectID.RayDetectTop(west);


    }
    public GameObject RayDetectFloor(Vector2 position)
    {

        Vector3 mousePos = Camera.main.ScreenToWorldPoint(position);

        RaycastHit2D hit = Physics2D.Raycast(position, Vector2.zero);

        RaycastHit2D[] hits;

        hits = Physics2D.RaycastAll(position, Vector2.zero);

        List<GameObject> hitObjectsList = new List<GameObject>();


        for (int i = 0; i < hits.Length; i++)
        {
            hitObjectsList.Add(hits[i].collider.gameObject);
        }

        GameObject[] HitObjectsArray = hitObjectsList.ToArray();


        if (hit.collider != null)
        {
            GameObject floorObject = GetFloorObject(HitObjectsArray);
            print("floor object is " + floorObject);
            return floorObject;
        }
        else
        {
            print("no object detected");
            return null;
        }
    }
    public IEnumerator CheckSurroundings()
    {
        //check northern block. 


        if (northObject != null)
        {

            connectedNorth = true;

            print("northern block is : " + northObject);
        }
        else
        {
            connectedNorth = false;

            Debug.Log("no northern block found.");
            noBlockCounter++;
        }


        //check southern block.

        if (southObject != null)
        {
            connectedSouth = true;



            print("southern block is : " + southObject);
        }
        else
        {
            connectedSouth = false;

            Debug.Log("no southern block found.");
            noBlockCounter++;
        }


        //check eastern block.

        if (eastObject != null)
        {
            connectedEast = true;

            print("eastern block is : " + eastObject);
        }
        else
        {
            connectedEast = false;

            Debug.Log("no eastern block found.");
            noBlockCounter++;
        }

        //check western block.

        if (westObject != null)
        {
            connectedWest = true;

            print("western block is : " + westObject);
        }
        else
        {
            connectedWest = false;

            Debug.Log("no western block found.");
            noBlockCounter++;
        }


        Debug.LogWarning("objects on this tile: " + blocksOnThisTile.Length);


        if (noBlockCounter == 4)
        {
            if(isDNA == false)
            {
                GetComponent<SpriteRenderer>().color = new Color(0.7f, 0, 0, 1);
                connectedToDNA = false;
                noBlockCounter = 0;
            }
            else
            {
                GetComponent<SpriteRenderer>().color = new Color(0, 1, 1, 1);
            }

        }
        else
        {

            for(int i = 0; i < blocksOnThisTile.Length; i++)
            {
                if (blocksOnThisTile[i] != null)
                {
                    blocksOnThisTile[i].GetComponent<UpdateBlock>().connectedToDNA = true;
                    blocksOnThisTile[i].GetComponent<SpriteRenderer>().color = new Color(0, 1, 1, 1);
                    blocksOnThisTile[i].GetComponent<UpdateBlock>().noBlockCounter = 0;
                    objectsOnTileFound++;
                }

            }

            objectsOnTileFound = 0;
        }



        blockChecked = true;


        yield return new WaitForSeconds(0.0001f);

        Debug.LogWarning("NORTHERN BLOCK IS: " + northObject);
        if (northObject != null)
        {
            if (northObject.GetComponent<UpdateBlock>().blockChecked == false)
            {
                StartCoroutine(northObject.GetComponent<UpdateBlock>().CheckSurroundings());
            }
        }
        yield return new WaitForSeconds(0.0001f);
        Debug.LogWarning("SOUTHERN BLOCK IS: " + southObject);

        if (southObject != null)
        {
            if (southObject.GetComponent<UpdateBlock>().blockChecked == false)
            {
               StartCoroutine(southObject.GetComponent<UpdateBlock>().CheckSurroundings());
            }
        }
        yield return new WaitForSeconds(0.0001f);
        Debug.LogWarning("EASTERN BLOCK IS: " + eastObject);

        if (eastObject != null)
        {
            if (eastObject.GetComponent<UpdateBlock>().blockChecked == false)
            {
               StartCoroutine(eastObject.GetComponent<UpdateBlock>().CheckSurroundings());
            }
        }
        yield return new WaitForSeconds(0.0001f);
        Debug.LogWarning("WESTERN BLOCK IS: " + westObject);

        if (westObject != null)
        {
            if (westObject.GetComponent<UpdateBlock>().blockChecked == false)
            {
                StartCoroutine(westObject.GetComponent<UpdateBlock>().CheckSurroundings());
            }
        }

        //surroundingsChecked = true;

        timesExecuted++;

        //NEED TO CHECK TIMES EXCECUTED
       // Debug.LogWarning("executed " + timesExecuted + " times.");
    }

0 个答案:

没有答案