我不明白什么(语句中的表达只能通过副作用执行)在我的代码中

时间:2017-02-08 13:50:50

标签: unity3d unityscript

#pragma strict
var lifetime = 5.0; //lifetime of projectile
var explosion : GameObject; //explosion prefab
var counter : int = 0; //keeps track of player score
var scoreToWin : int = 20; //determines score for player to win
var col : Collider; 

void; OnTriggerEnter(Collider, col);
     {
     //triggers collision on enemy tag
     if(col.gameObject.tag == "Enemy")
     {
       Score();     
       Debug.Log("Score!");
       Destroy(col.gameObject);
    //destroys enemy object
        var explo = Instantiate(explosion, transform.position, Quaternion.identity);
 //destroy the projectile that just caused the trigger collision
    Destroy(explo, 3); // delete the explosion after 3 seconds
 Destroy(gameObject,lifetime);
     }
}

function update()
{
guiText.text = "Score: "+counter;

}

function Score()
{
counter++;
    if (counter == scoreToWin);
            "setTimeout(10000, 5000)";
            Debug.Log (" You Win");
            Application.Quit;
}

Picture of Code

经过一些研究后我发现错误意味着代码行没有做任何事情。但我不明白这是怎么回事。只要我的碰撞,超时和app.quit的逻辑是正确的。我也试图找到一种方法来实现我的guiText到屏幕上观看玩家

3 个答案:

答案 0 :(得分:2)

还有一些问题,但主要问题是你在错误的地方使用分号。他们在这里:

void; OnTriggerEnter(Collider, col); // Two incorrect semi-colons. Incorrect comma.

// Further down:

if (counter == scoreToWin); // Incorrect semi-colon
        "setTimeout(10000, 5000)"; // What's this expected to do?
        Debug.Log (" You Win"); // OK
        Application.Quit; // Quit is a method

修正

你可能意味着:

void OnTriggerEnter(col : Collider)
{
 // ...

if(counter == scoreToWin)
{
    Debug.Log("You Win");
    Application.Quit();
}

我该怎么用;?

;表示语句的结尾,当该语句具有(花括号)时,不会使用它。 void Hello(){}一个语句,但它有大括号,因此不需要分号。 var score=5;是一个没有大括号的陈述,所以最后会使用分号。

作用域

看起来你习惯使用类似Python的东西,其中缩进也代表范围。 C#和UnityScript(" javascript")不喜欢这些语言。

if(score == max)
{
   // Don't forget those curly brackets!
   // Everything that happens when the score is 'max' goes in here.
}

但是,有一个实例,其中不需要那些花括号 - 这是隐式花括号

if(score == max)
    doSomething(); // *only* this line runs if score is max. Watch out!
    Debug.Log("No matter what score is, this shows up!");

隐含括号时,if为真时只运行第一行。

如果你没有在那里放置分号,你可能会对debug.log一直出现的原因感到非常困惑,所以我建议使用大括号总是 - 至少在你对这门语言充满信心之前。

答案 1 :(得分:0)

看起来你有太多的冒号(语句终结符)。

尝试更改此内容:

if (counter == scoreToWin);
        "setTimeout(10000, 5000)";
        Debug.Log (" You Win");
        Application.Quit;

对此:

if (counter == scoreToWin)
{
        setTimeout(10000, 5000);
        Debug.Log (" You Win");
        Application.Quit;
}

此函数定义中的类似问题:

void; OnTriggerEnter(Collider, col);

答案 2 :(得分:0)

问题在于:

  • void;什么都不做,这是一个不能单独使用的保留字。
  • "string literal"什么都不做,你必须把它变成一个变量或至少用它来做什么
  • functionname无需在最后使用()调用它。

这就是你的骗子所抱怨的。