当玩家(另一个2D精灵)统一进入扳机时,我如何使硬币(一个2D精灵)自毁?

时间:2019-12-09 18:31:35

标签: c# unity3d

我在排序第6层上都有它们(在sprite渲染器上) 我不太确定自己做错了什么。 这是我目前所拥有的硬币:

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.name == "Player")
    {
        Debug.Log("Collided");
        Destroy(this.gameObject);
    }
}

1 个答案:

答案 0 :(得分:0)

您没有更改分数的值。您要更改的值是游戏得分的副本,而不是实际得分。

float gingerscore = Gamemanager.score; //Creates a new float, with the same value as Gamemanager.score
gingerscore--;//Decrease the newly created float's value by one, but not the original object.

您需要做的是将gingerscore重新分配给Gamemanager.score。或像这样直接编辑Gamemanager.score值:

//This method is a bit roundabout, but will work and is closest to what you try currently
float gingerscore = Gamemanager.score; //Make a copy of score
gingerscore--;//Decrement the value
Gamemanager.score = gingerscore; //Assign the new value back to score

//This is a more straightforward approach to doing the same thing
Gamemanager.score--;//Decrement the score directly

您可能想了解在C#中按值或按引用复制数据之间的区别。乔恩·斯基特(Jon Skeet)的文章不错on it here

(为了提高代码的可读性,请注意。以大写字母开头的新单词。例如gingerScoreGameManager。C#的命名约定可以为found here