为什么分数在 unity C# 中不能正常工作 [英] Why the score didn't work properly in unity C#

查看:83
本文介绍了为什么分数在 unity C# 中不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个基于破坏球的计分系统,所以一开始,分数是82,每破坏一个球,分数就会减少1.

I made a scoring system based on destroyed balls, so on start, the score is 82, and each ball destroyed should decrease the score by 1.

我为球写了一个脚本,但得分不正常,你能告诉我我的代码有什么问题吗?

i made a script for the ball, but the score didn't work properly, can you tell me what's wrong in my code?

这是球的脚本:

public class Ball : MonoBehaviour {

    private int Count;
    public Text TextCount;

    // Update is called once per frame
    Rigidbody rb;
    public float destroyTimeOut = 2;
    bool hitBase = false;
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        Count = 82;
        SetTextCount();
    }


    void Update () {
        if(rb.position.y < -3)
        {
            Destroy(gameObject);
            Count = Count - 1;
            SetTextCount();

        }
        if (hitBase)
        {
            timer += Time.deltaTime;
            if (timer > destroyTimeOut)
            {
                Destroy(gameObject);
                Count = Count - 1;
                SetTextCount();

            }
        }
    }
    float timer;
    private void OnCollisionEnter(Collision collision)
    {

        if (collision.gameObject.CompareTag("base"))
        {
            hitBase = true;
        }
    }

    void SetTextCount()
    {
        TextCount.text = Count.ToString();
    }

}

谢谢,

推荐答案

当你调用 Destroy(gameObject) 时,这个游戏对象会被这个附加到的 Ball 脚本销毁

When you call Destroy(gameObject) this GameObject is destroyed with this Ball script that is attached to it.

将您的评分系统与球碰撞检测系统分开.

Separate your score system from the ball collision detection system.

1.创建一个空游戏对象并将其命名为ScoreSystem".

1.Create an Empty GameObject and name it "ScoreSystem".

2.创建一个脚本并将其命名为ScoreSys",然后在其中使用下面的代码.

2.Create a script and name it "ScoreSys" then use the code below inside it.

3.将其附加到ScoreSystem".游戏对象.

3.Attach it to the "ScoreSystem". GameObject.

public class ScoreSys : MonoBehaviour
{

    public Text TextCount;
    private int _Count;

    public int Count
    {
        get
        {
            return _Count;
        }
        set
        {
            if (_Count != value)
            {
                _Count = value;
                TextCount.text = _Count.ToString();
            }
        }
    }

    void Start()
    {
        Count = 82;
        TextCount.text = _Count.ToString();
    }
}

得分系统现在有自己的代码和游戏对象,不会被破坏.此外,Text 也会随着分数变化而更新.

The score system now has it's own code and GameObject and it won't be destroyed. Also, Text is also updated when score changes.

现在,您还必须分离 Ball 脚本.只需找到ScoreSystem"游戏对象,获取附加到它的 ScoreSys 组件并更新分数.Ball 脚本应该附加到将被销毁的 Ball 对象.

Now, you also have to separate the Ball script. Simply find the "ScoreSystem" GameObject, get the ScoreSys component attached to it and updated the score. The Ball script should be attached to the Ball Object that will be destroyed.

请注意,我不知道分数应该何时更新,但以下是您当前代码的翻译.您可能需要进行一些更改才能使其正常工作.

Note that I have no idea when the score should update but below is the translation of your current code. You may need to make some changes to get it working properly.

public class Ball : MonoBehaviour
{

    ScoreSys scoreSys;
    Rigidbody rb;
    public float destroyTimeOut = 2;
    bool hitBase = false;
    void Start()
    {
        GameObject scoreObj = GameObject.Find("ScoreSystem");
        scoreSys = scoreObj.GetComponent<ScoreSys>();

        rb = GetComponent<Rigidbody>();
    }


    void Update()
    {
        if (rb.position.y < -3)
        {
            scoreSys.Count--;
            Destroy(gameObject);
        }

        if (hitBase)
        {
            timer += Time.deltaTime;
            if (timer > destroyTimeOut)
            {
                scoreSys.Count--;
                Destroy(gameObject);
            }
        }
    }

    float timer;
    private void OnCollisionEnter(Collision collision)
    {

        if (collision.gameObject.CompareTag("base"))
        {
            hitBase = true;
        }
    }
}

这篇关于为什么分数在 unity C# 中不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆