c# - 找不到类型或命名空间名称 Unity
问题描述
我正在为我的 fps 游戏编写脚本,但我仍然收到此错误“找不到类型或命名空间名称‘目标’。” 我知道这行代码有问题,ReactiveTarget target = hitObject.GetComponent<Target>();
但我不知道它找不到这个 Target 类
using UnityEngine;
using System.Collections
public class Shooting : MonoBehaviour{
private Camera _camera;
void Start(){
_camera = GetComponent<Camera>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void OnGUI()
{
int size = 12;
float posX = _camera.pixelWidth / 2 - size / 4;
float posY = _camera.pixelHeight / 2 - size / 2;
GUI.Label(new Rect(posX, posY, size, size), "*");
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
Ray ray = _camera.ScreenPointToRay(point);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
GameObject hitObject = hit.transform.gameObject;
ReactiveTarget target = hitObject.GetComponent<Target>();
if (target != null)
{
target.ReactToHit();
}
else
{
StartCoroutine(SphereIndicator(hit.point));
}
}
}
}
private IEnumerator SphereIndicator(Vector3 pos)
{
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = pos;
yield return new WaitForSeconds(1);
Destroy(sphere);
}}
这是目标脚本
using UnityEngine;
using System.Collections;
public class Target : MonoBehaviour
{
public void ReactToHit()
{
StartCoroutine(Die());
}
private IEnumerator Die()
{
this.transform.Rotate(-75, 0, 0);
yield return new WaitForSeconds(1.5f);
Destroy(this.gameObject);
}
}
解决方案
你好
这里有问题(或者至少这是我的想法)
你是如何使用数据类型ReactiveTarget来获取Target的数据类型的?
这是我第一次看到这样的东西!
尝试改变
ReactiveTarget target = hitObject.GetComponent<Target>();
到:
Target target = hitObject.GetComponent<Target>();
推荐阅读
- javascript - javascript while循环问题,break不会停止循环
- java - 如何将 Java json 对象发送到 R 函数?
- reactjs - 在 React Query 中,如果我在组件中使用两个 mutate 函数,如何获取数据?
- json - JSON-Schema 有条件地向项目添加属性
- ios - 带有 .mov 的媒体文件不在 Cordova 应用程序中播放
- javascript - 基于数据属性的d3节点子结构/样式
- c# - 在 xaml 中从另一个项目添加 UserControl
- python - 模拟文件中导入的类的 __init__
- flutter - 什么是 ”?。” 颤振中的操作员
- vue.js - 循环内的 Vuetify 文本字段在一次输入后失去焦点