Unity3d 每次点击屏幕时都会加载GameOver场景

Unity3d 每次点击屏幕时都会加载GameOver场景,unity3d,unityscript,Unity3d,Unityscript,首先,这是我的剧本: 布拉格严格 var不正确:音频剪辑; var-correct:音频剪辑; var:游戏对象; 钻石:雪碧; 变圆:雪碧; 变量三角形:精灵; var square:Sprite; 变量编号:int; var-AcceptInput:boolean=true; 静止的 变量得分:int=1; guiScore:吉他文本; 功能启动{ 数字=随机。范围1,4; 如果数字==1{ anything.getComponentSpritender.sprite=diamond; }如

首先,这是我的剧本:

布拉格严格 var不正确:音频剪辑; var-correct:音频剪辑; var:游戏对象; 钻石:雪碧; 变圆:雪碧; 变量三角形:精灵; var square:Sprite; 变量编号:int; var-AcceptInput:boolean=true; 静止的 变量得分:int=1; guiScore:吉他文本; 功能启动{ 数字=随机。范围1,4; 如果数字==1{ anything.getComponentSpritender.sprite=diamond; }如果数字=2,则为else{ anything.GetComponentSpritender.sprite=圆; }如果数字=3,则为else{ anything.getComponentSpritender.sprite=三角形; }否则{ anything.getComponentSpritender.sprite=square; } } 功能更新{ 如果Input.GetMouseButtonDown0{ 如果接受输入{ AcceptInput=false; Debug.LogClicked; 如果有任何问题,则返回.GetComponentSpritender.sprite==菱形{ audio.PlayOneShotcorrect; guiScore.text=分数:+Score; StartRoutineYieldTester分子; AcceptInput=true; 数字=随机。范围1,4; 如果数字==1{ anything.getComponentSpritender.sprite=diamond; }如果数字=2,则为else{ anything.GetComponentSpritender.sprite=圆; }如果数字=3,则为else{ anything.getComponentSpritender.sprite=三角形; }否则{ anything.getComponentSpritender.sprite=square; } }else如果有的话。getComponentSpritender.sprite==圆圈{ 错误答案!; Application.LoadLevelGameOver; audio.playoneshot不正确; }else如果有的话。GetComponentSpriteEnder.sprite==三角形{ 错误答案!; Application.LoadLevelGameOver; audio.playoneshot不正确; }else如果有的话。GetComponentSpriteRenderer.sprite==square{ 错误答案!; Application.LoadLevelGameOver; audio.playoneshot不正确; } } } } 功能分子{ 收益率为0.5秒; }
正如你所看到的,这是一个选择题游戏。出于一些愚蠢和奇怪的原因,如果我按屏幕上的任何地方,它只会加载GameOver场景,即使我得到了正确的答案。为什么它会在屏幕上的任何地方检测到触摸?这太奇怪了。你们中有谁知道如何解决这个问题吗?我不明白。任何帮助都将不胜感激。

将碰撞器2D附加到您的任何东西上,并与

OnMouseDown()
{
    var lSprite : Sprite ;
    lSprite = GetComponent(SpriteRenderer).sprite  ;
    if (lSprite == diamond) {
        audio.PlayOneShot(correct);
        guiScore.text = "Score: " + score;
        StartCoroutine("YieldTestEnumerator");

        AcceptInput = true;
        number = Random.Range(1, 4);

        if (number == 1) {
            lSprite = diamond;
        } else if (number == 2) {
            lSprite = circle;
        } else if (number == 3) {
            lSprite = triangle;
        } else {
            lSprite = square;
        }

    } else 
    if (lSprite == circle) {

        Debug.Log("Wrong Answer!");
        Application.LoadLevel("GameOver");
        audio.PlayOneShot(incorrect);

   } else 
   if (lSprite == triangle) {

        Debug.Log("Wrong Answer!");
        Application.LoadLevel("GameOver");
        audio.PlayOneShot(incorrect);

   } else
   if (lSprite == square) {

        Debug.Log("Wrong Answer!");
        Application.LoadLevel("GameOver");
        audio.PlayOneShot(incorrect);

   }

}

请在适当位置缩进4个空格,以修正缩进。现在你的代码几乎无法阅读。这样更好吗@不太好,但我想我已经修好了。看看差别,确保它仍然正确。第一个是免费的;所以我假设您只希望在单击特定项时执行操作?我在你的代码中看不到任何东西,你检查是否是这样的。你现在只对点击做出反应,不做进一步的过滤。所以我想做的是当你按下按钮时,检查另一个物体的精灵是否是钻石。如果是,则加1分。然后再把精灵变回原形。如果不是,那么加载场景gameover,现在,出于某种奇怪的原因,我按屏幕上的任意位置,它会带我到gameover场景@Bart你在点击任何东西吗?不,我为任何东西设置变量的原因是因为它是不同的对象。我给你看的脚本是一个按钮,如果你点击它,它会检查精灵是否有任何东西并添加分数。我尝试将一个碰撞的R2D添加到其他东西,但没有成功。脚本必须附加到一个有碰撞的R2D的对象,并且你必须单击该对象。要确保您正在单击对象,请添加调试。单击日志!到OnMouseDown的开始不不不你不明白我在说什么。上面的脚本是一个按钮,就像选择题游戏中的按钮一样