0
    do{
    if(other.transform.tag == "Player"){ //checking to see who bumps into it(PlayerShip)
        Vector3 currentPos = transform.position; //grabbing the position of player
        string currentTag = transform.tag; //also grabbing it's tag
        if(currentTag == "a_large"){//based on which asteroid is hit, it does something different
                GameController.lives1--;

        }
        else if(currentTag == "a_medium"){
                GameController.lives1--;
        }
        else if(currentTag == "a_small"){
                GameController.lives1--;

        }

    }
    }
    while(GameController.lives1 > 0);

This is my script. I did the same thing for Score (except it increments, and no do while loop) and it works fine. For some reason though when my collider other with tag "Player" hits asteroid with tag 'a_large' the 'int lives1' goes to 0. I think my syntax is off just trying to get some feedback at how I can fix this remedy.

4

1 回答 1

1

只是为了完全澄清出了什么问题:

您正在使用循环来防止 的值lives1到达0。与您预期不同的是,您立即递减lives10. 为什么?

当您的代码进入循环时,currentTag == "a_large"true. 请注意,currentTag您的循环中永远不会更新。

在第一次迭代中,currentTag仍然是a_large,所以lives1减少了。第二次迭代, currentTagwas still a_large,所以又一次递减。重复直到最终达到循环的中断条件,即lives1 <= 0.

正如@dwerner 所说,只需使用if( lives1 == 0 )语句来检查lives1存在0

于 2013-11-07T19:50:15.893 回答