所以我试图将鼠标单击的对象保存到一个单独的变量,但RaycastHit不会转换为GameObject,即使在检查其类型的if语句中也是如此。C#故障设置变量
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Select : MonoBehaviour
{
public GameObject selectorPrefab;
private GameObject selectedObject;
private GameObject clone;
void Update()
{
if(Input.GetMouseButtonDown(0))//left click
{
if(clone)
{
Destroy(clone);
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && hit.collider.tag == "Ship")
{
Vector3 position = hit.transform.position;
float scaleMultiplier = (hit.transform.localScale.x + hit.transform.localScale.z)/2;
clone = Instantiate(selectorPrefab);
clone.transform.position = position;
clone.transform.localScale *= scaleMultiplier;
if(hit is GameObject)//Green underline here
{
selectedObject = hit;//Red underline under "hit"
}
}
}
}
}
您可能想要添加相关的技术/ API /框架标签。 – Plutonix
selectedObject = hit.transform.gameObject –
您在红色下划线处的错误消息可能是无法从一种类型分配到另一种类型。你需要对它进行转换,比如'selectedObject =(GameObject)命中;' – ClickRick