如何更改触发器上的 material?

How to change the material on a trigger?

我正在做名为“Roll-a-ball”的 Uniy 教程,试图进行一些更改。现在尝试更改框的 material 0.5 秒 在你收集它们之前被移除之前,但我对此有点迷茫。

我的想法是在 OnTriggerEnter 方法中完成,当你收集它们时,在玩家控制的球体脚本中移除它们。

这是我的方法。这里我去掉框,增加分数:

private void OnTriggerEnter(Collider other) {
    Destroy(other.gameObject);
    puntos += 1;
    IncrementarPuntos();
}

知道我该怎么做吗?

获取对象上的Renderer组件,然后设置渲染器的material。类似于:

public Material collectMaterial; // This will allow you to change the material in the inspector

private void OnTriggerEnter(Collider other) {
    other.GetComponent<Renderer>().material = collectMaterial;
    puntos += 1;
    IncrementarPuntos();
    Destroy(other.gameObject, 0.5f); // Destroy after 0.5 seconds.
}

需要注意的是玩家是否在它被摧毁之前多次尝试收集它。要解决此问题,您可能希望在末尾使用 Destroy(this) 销毁脚本,但如果您仍需要脚本的另一部分,还有其他方法可以禁用拾取它。