【发布时间】:2019-11-19 18:19:19
【问题描述】:
所以玩家点击一个按钮来创建一个盒子。我需要这个盒子在几种颜色之间随机变化。我还需要这个盒子有一个与所述颜色相对应的标签。绿框 - “greenBlock”标签等
我已经实例化了这个盒子,然后尝试用 material.color 改变它的材质。它什么也没做。我已经看到了 sharedMaterial 的建议,但尝试过发现它最终会改变场景中每个游戏对象的颜色。我想我正确地获取了盒子预制渲染器?任何帮助将不胜感激!
这是我目前所拥有的:
public class ButtonScript : MonoBehaviour
{
public GameObject Box;
public Transform spawnpoint1;
public Transform spawnpoint2;
public Rigidbody2D player;
public Renderer boxRenderer;
[SerializeField]
private Color boxColor;
[SerializeField]
private AudioSource actionSound;
// Update is called once per frame
private void Start()
{
//boxRenderer = Box.gameObject.GetComponent<Renderer>();
boxRenderer = GameObject.Find("Box").GetComponent<Renderer>(); // Find the renderer of the box prefab
}
public void OnTriggerStay2D(Collider2D col)
{
if (player) // If it's the player in the collider trigger
{
if (Input.GetKeyDown(KeyCode.E))
{
Instantiate(Box, spawnpoint1.position, Quaternion.identity);
boxRenderer.material.color = boxColor; // change the color after it is instantiated
actionSound.Play();
}
}
}
}
【问题讨论】: