检查光线投射的命中率是否会导致游戏崩溃?

时间:2018-12-20 20:53:16

标签: unity3d

所以我试图检测我是否单击了带有标签“ solarsystem”的对象,如果是,则将该太阳系加载到另一个场景上。这段代码之前工作得非常好,但是现在它以某种方式崩溃,我不得不从任务管理器中终止并使用结束任务按钮才能关闭它。它只是完全停止响应。

这是我认为在弄乱许多Debug.log之后找到错误的代码,以查找代码停止的位置,从而找出统一停止响应的位置:

    RaycastHit[] hit = Physics.RaycastAll(cursorPosition, Vector3.forward,15f);
        Debug.Log("test2");//this is printed to the console - code crashes below this line
        for(int i = 0; i < hit.Length; i++)
        {
            Debug.Log("hit"); // this is never printed to console - code crashes above this line
            if(currentScene == "Universe")
            {
                if(hit[i].collider.gameObject.tag == "SolarSystem")
                {
                    ChangeScene("SolarSystem");
                    SolarSystem clickedSolarSystem = hit[i].collider.gameObject.GetComponent<SystemObjectLink>().LinkedClass;
                    SolarSystem LoadedSolarSystem = SolarSystemCamera.GetComponent<SolarSystem>() as SolarSystem;
                    LoadedSolarSystem = clickedSolarSystem;
                    Debug.Log("generating system clicked on");
                    if (LoadedSolarSystem.preGenerated == false)
                    {
                        LoadedSolarSystem.Generate();
                    }
                    else
                    {
                        LoadedSolarSystem.Regenerate();
                    }
                    break;
                }
            }
            if(currentScene == "SolarSystem")
            {
                if (hit[i].collider != null)
                {
                    if (hit[i].collider.gameObject.tag == "Planet")
                    {
                        Target = hit[i].collider.gameObject;
                        break;
                    }
                    else if (hit[i].collider.gameObject.tag == "Moon")
                    {
                        Target = hit[i].collider.gameObject;
                        break;
                    }
                    Target = hit[i].collider.gameObject;
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我有一个for(;;)语句,带有硬编码

    if(<state>){
        break:
    }

声明打破无限循环。但是当我单击游戏中的一个对象时。它会在运行之前检查代码是否有错误。当它做到这一点时,它陷入了无限循环,所以要解决它,我做到了

    for(;errorIndex<99; errorIndex++){

    }

我的错误和所学到的知识

切勿使用while(true)循环或for循环,而无法摆脱(for(;;)

如果统一引擎/编辑器停止响应,这是因为它在无限循环中进行了处理-查看您的代码,并确保任何循环都不可能永远持续下去