Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 计分制_C#_Unity3d - Fatal编程技术网

C# 计分制

C# 计分制,c#,unity3d,C#,Unity3d,我看了一个教程,正试图为我的unity项目建立一个计分系统。然而,这些分数并没有被制成表格。我创建了一个AddScore()函数,并将其包含在PlayerBullet.cs中(当玩家子弹与敌人发生碰撞时,其中的OnTriggerEnter2D函数)。inspector窗口中的分数点保持为0。有人能帮我吗?谢谢 PlayerBullet.cs代码: public int Score; //this function will be triggered when there is

我看了一个教程,正试图为我的unity项目建立一个计分系统。然而,这些分数并没有被制成表格。我创建了一个AddScore()函数,并将其包含在PlayerBullet.cs中(当玩家子弹与敌人发生碰撞时,其中的OnTriggerEnter2D函数)。inspector窗口中的分数点保持为0。有人能帮我吗?谢谢

PlayerBullet.cs代码:

    public int Score;

    //this function will be triggered when there is a collision between the player bullet and enemy 
    void OnTriggerEnter2D(Collider2D collide)
    {
        //detects when the player's bullet collide with the enemy 
        if(collide.tag == "EnemyTag")
        {
            //destroy the player bullet 
            Destroy(gameObject);
            AddScore();
        }
    }

    void AddScore()
    {
        Score++;
    }
试试这个

public static int Score;

//this function will be triggered when there is a collision between the player bullet and enemy 
void OnTriggerEnter2D(Collider2D collide)
{
    //detects when the player's bullet collide with the enemy 
    if(collide.tag == "EnemyTag")
    {
        Score++;
        Debug.Log("Score: " + Score); 
        //destroy the player bullet 
        Destroy(gameObject);
    }
}

也许您需要一些UI文本来显示它?inspector窗口中的分数不会增加以打印一些日志到控制台?也许您从未调用AddScore()此函数
Score
变量在被销毁的类别中。应将其设置为静态,如建议的答案中所示,或引用另一个未销毁的类别中的int变量,并将分数添加到销毁之前。调试日志显示在控制台中。但它不起作用意思是,分数不是更多?