unity3d多个物体情况,点击鼠标如何选择其中一个物体?用c#如何写代码

unity3d多个物体情况,如几个cube,鼠标点击对应cube,此时该cube成为我选择的对象?用c#如何写代码

先给物体添加碰撞器
下面代码当鼠标点击带碰撞器的物体时显示该物体名称。
if(input.GetmouseButton(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition)
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
debug.log(hit.Name)
}

}追问

Type `UnityEngine.RaycastHit' does not contain a definition for `Name' and no extension method `Name' of type `UnityEngine.RaycastHit' could be found

追答

写掉了个collider
debug.log(hit.collider.Name)

温馨提示:答案为网友推荐,仅供参考