我发布了一款游戏在苹果应用商店上,使用Unity开发四年了,除了不断提供bug修复外,我还尝试解决一个只有在iOS设备上才会出现的bug。这个bug在编辑器中是没有问题的。

在我的脚本中,我有一个Update()函数,代码如下:

private void Update()
{
    // get the ball's rigidbody if we don't have it already
    if (playerBall == null || playerBallBody == null)
    {
        playerBall = GameObject.FindWithTag(Tags.PLAYERS_BALL_TAG);
        playerBallBody = playerBall.GetComponent<Rigidbody>();
    }

    // return if the ball's rigidbody is still null
    if (playerBall == null || playerBallBody == null) return;

    // if the gameOverUI has respawnedAfterAd
    if (GameOverManager.instance.respawnedAfterAd)
    {
        canThrow = true;
        thrown = false;
    }

    // if we can throw
    if (canThrow)
    {
        // handle different ball throw modes
        if (legacyBallThrow)
            CheckForValidLegacyBallThrow();
        else if (pullbackBallThrow)
            HandlePullbackBallThrowType();
    }
    else { // if we can't throw set attemptingThrowing to false
        attemptingThrowing = false;
    }

    // if callThrowBall then ThrowBall()
    if (callThrowBall)
    {
        ThrowBall(attemptedThrowDistanceY);
        callThrowBall = false;
    }

    // if the ball y pos is greater than 28 then rebound the ball
    if (playerBall.transform.position.y > 28f && !ballRebounded)
    {
        playerBallBody.velocity = new Vector3(playerBallBody.velocity.x, -playerBallBody.velocity.y * 0.4f,
            playerBallBody.velocity.z * 1.15f);

        ballRebounded = true;
    }
}

这个bug会在我尝试调用ThrowBall()函数时出现,会抛出NullReferenceException。错误的原因是playerBallBody为null。然而,我已经尝试了很多方法来解决这个问题,但仍然没有找到原因。

原因有两个:

  1. 在编辑器中,这个脚本是正常工作的。在运行时,我也可以看到这个对象有一个rigidbody。但是,只有在iOS设备上才会出现这个bug。

  2. 这个bug只会在特定的球皮肤上出现(在我的游戏中,球皮肤是不同的prefab)。但是,这个prefab确实有一个rigidbody。

我已经确保了编译的代码没有被剥离,但这并不是问题的原因。所以,我真的需要您的帮助来解决这个问题!