最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Unity C#. void Start() does work in editor but doesn't in build - Stack Overflow

programmeradmin0浏览0评论

I have a grid based movement system. The for-loop runs through every tile and checks it is occypied (first if) and the attitude (second if). The problem is: this code is working perfectly in editor, but in build the second if just isn't being executed. I put it first in the script execution order so it doesn't conflict with other scripts. I've been trying to solve it for 3 days. Just don't know what else I could do. Thanks :)

void Start()
{
    WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);

    tileSize = Math.Abs(referenceObject1.transform.position.x - referenceObject2.transform.position.x);

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 0, j * tileSize), Vector3.up, out RaycastHit hit2, tileSize * 10) && hit2.collider.CompareTag("Obstacle"))
            {
                WalkGrid.gridArray[i, j].isOccupied = true;
            }

            if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 9, j * tileSize), Vector3.down * 10, out RaycastHit hit, 10))
            {
                WalkGrid.gridArray[i, j].attitude = hit.point.y;
            }
        }
    }
}
  1. Script execution order
  2. Put it in 2 different for loops
  3. Used Awake instead of start (was even worse)

I have a grid based movement system. The for-loop runs through every tile and checks it is occypied (first if) and the attitude (second if). The problem is: this code is working perfectly in editor, but in build the second if just isn't being executed. I put it first in the script execution order so it doesn't conflict with other scripts. I've been trying to solve it for 3 days. Just don't know what else I could do. Thanks :)

void Start()
{
    WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);

    tileSize = Math.Abs(referenceObject1.transform.position.x - referenceObject2.transform.position.x);

    for (int i = 0; i < width; i++)
    {
        for (int j = 0; j < height; j++)
        {
            if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 0, j * tileSize), Vector3.up, out RaycastHit hit2, tileSize * 10) && hit2.collider.CompareTag("Obstacle"))
            {
                WalkGrid.gridArray[i, j].isOccupied = true;
            }

            if (Physics.Raycast(WalkGrid.gridStart + new Vector3(i * tileSize, 9, j * tileSize), Vector3.down * 10, out RaycastHit hit, 10))
            {
                WalkGrid.gridArray[i, j].attitude = hit.point.y;
            }
        }
    }
}
  1. Script execution order
  2. Put it in 2 different for loops
  3. Used Awake instead of start (was even worse)
Share Improve this question edited Feb 3 at 20:06 Ferry To 1,5252 gold badges38 silver badges55 bronze badges asked Feb 1 at 16:33 MichaelMichael 1 6
  • 1 Only thing i can imagine is it is throwing an error and not reporting it. Have you tried the extra traditional method of a ton of debug messages and seeing what appears in log file when you run the build? – BugFinder Commented Feb 1 at 16:48
  • 1 Used Awake instead of start (was even worse) .. how was it worse? – derHugo Commented Feb 1 at 17:03
  • How exactly do you know the second if is failing? As mentioned above do some Debugging .. hard to tell from here with the information we have – derHugo Commented Feb 1 at 17:04
  • What do you mean by does work in editor? What editor? What does work? – Sergey A Kryukov Commented Feb 1 at 17:56
  • Thank you a lot. After a little debugging I could define the exact problem: in build RayCast doesn't trigger mesh colliders. All box colliders were triggered. I've read that raycasts can have issues with mesh colliders but how should I fix it. After correct script execution order awake and start give both the same result. – Michael Commented Feb 1 at 19:29
 |  Show 1 more comment

2 Answers 2

Reset to default 0

Your issue is most likely related to your attempt to do physics interactions before any physics update right at the start of the game. You could try calling Physics.SyncTransforms at the beginning of Start. Though waiting at least one frame may still be necessary. This could be done by turning Start into a coroutine.

IEnumerator Start()
{
    yield return null;
    Physics.SyncTransforms();

    WalkGrid = new WalkGrid(width, height, referenceObject1, referenceObject2);
    // [ ... ]

The solution is quiet strange. I just reimported the model with mesh collider and it worked! Maybe it was a bug while importing although the issue was only in the build

发布评论

评论列表(0)

  1. 暂无评论