using UnityEngine; using System.Collections; public class task : MonoBehaviour { Ray ray; RaycastHit _hit; GameObject cube; Material color1; // Use this for initialization void Start() { //创建一个方块 cube = GameObject.CreatePrimitive(PrimitiveType.Cube); color1 = cube.GetComponent().material; } // Update is called once per frame void Update() { //获取鼠标射线 ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out _hit)) { if (_hit.transform.gameObject.GetComponent()) { //随机一种颜色 Color color = new Color(Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f, Random.Range(0f, 1f) * Time.deltaTime * 30f); //给方块添加上颜色 color1.color = color; } } } }