简体   繁体   English

如何在 OnTriggerEnter 中增加一个数字

[英]how can i increase a number inside OnTriggerEnter

so i'm trying to increase my score every time i hit a wall (IsTrigger) but when i try to increase it, it doesn't work and still 100所以我每次撞墙时都会尝试增加分数(IsTrigger),但是当我尝试增加分数时,它不起作用,仍然是 100

so this is the code im having problem with所以这是我遇到问题的代码

    public Text scoreText;
    public int score = 0;

    void Start()
    {
        scoreText.text = score.ToString();
    }
    // Add Some Score To The Text When The Player Hit The CheckPoint
    void OnTriggerEnter(Collider collider)
    {
        if (collider.name == "Player")
        {
            score += 100;
            scoreText.text = score.ToString();
            Debug.Log(score);
        }
    }

When doing the trigger/collision test, make sure you add one Debug.Log() outside of the condition check.在进行触发/碰撞测试时,请确保在条件检查之外添加一个Debug.Log() In your case,在你的情况下,

void OnTriggerEnter(Collider collider)
{
    if (collider.name == "Player")
    {
        score += 100;
        scoreText.text = score.ToString();
        Debug.Log(score);
    }
    Debug.Log("Just want make sure it indeed triggered.");
}

In most case, other than the issue with the code, it is highly possible the Rigidbody component is not added to the GO.在大多数情况下,除了代码问题之外,极有可能没有将刚体组件添加到GO

Another part which I am not sure since I am not using Unity for quite a long time now.另一部分我不确定,因为我已经很长时间没有使用 Unity 了。 You can move the UI text update code outside of the trigger if you want.如果需要,您可以将 UI 文本更新代码移到触发器之外。

You need to update the text after changing the value, in your case you have to update "scoreText" according to the new "score" value.您需要在更改值后更新文本,在您的情况下,您必须根据新的“score”值更新“scoreText”。 The simple way to do it is like that:简单的方法是这样的:

    public Text scoreText;
    public int score = 0;

    void Start()
    {
        scoreText.text = score.ToString();
    }
    // Add Some Score To The Text When The Player Hit The CheckPoint
    void OnTriggerEnter(Collider collider)
    {
        if (collider.name == "Player")
        {
            score += 100;
            scoreText.text = score.ToString();
            Debug.Log(score);
        }
    }

It is better to have a method called "ChangeScore" which includes updating the "score" value and updating the "scoreText" at the same time.最好有一个名为“ChangeScore”的方法,其中包括更新“score”值和同时更新“scoreText”。 It is better to do so since you might forget to update the text in other cases if you have to change the score somewhere else.最好这样做,因为如果您必须在其他地方更改分数,您可能会忘记在其他情况下更新文本。 Example:例子:

    [Tooltip("The player's initial score.")]
    [SerializeField] private int initScore = 0;
    
    // Serialized since you have to set the reference for it.
    [SerializeField] private Text scoreText;

    private int score = 0;

    void Start()
    {
        ChangeScore(initScore);
    }

    // Method used to change the current player's score and update the text.
    void ChangeScore(int changeValue)
    {
        this.score += changeValue;
        scoreText.text = score.ToString();
    }
    
    // Add Some Score To The Text When The Player Hit The CheckPoint
    void OnTriggerEnter(Collider collider)
    {
        if (collider.gameObject.name == "Player")
        {
            ChangeScore(100);
            Debug.Log(score);
        }
    }

Another side note: I wouldn't use tags or names while checking colliding, its better to check if the colliding object got the PlayerController script另一个注意事项:在检查碰撞时我不会使用标签或名称,最好检查碰撞 object 是否有 PlayerController 脚本

if(collider.GetComponent<PlayerController>())

instead of代替

if (collider.gameObject.name == "Player")

i fixed it i put the code on the Player script not on the CheckPoint script我修复了它我把代码放在Player脚本上而不是CheckPoint脚本上

    void OnTriggerEnter(Collider collider)
    {
        if (collider.tag == "ScoreCheckPoint")
        {
            score += 100;
            scoreText.text = score.ToString();
            Debug.Log(score);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM