gpt4 book ai didi

c# - Unity找不到其他GameObject的脚本

转载 作者:太空狗 更新时间:2023-10-29 21:38:38 25 4
gpt4 key购买 nike

我在从另一个 GameObject 的脚本获取变量时遇到问题。我以前使用过这种类型的引用,我知道它是如何工作的,但出于某种原因,它说它找不到我所引用的脚本。

引用其他代码的代码(CanHearPlayer() 开头的 if 语句):

using UnityEngine;
using System.Collections;

public class EnemySight : MonoBehaviour {

public GameObject Player;
public float fieldOfViewDegrees = 30;
public float visibilityDistance = 50;
public bool SeeingPlayer;
public float deathDistance;
public float hearDistance;

void Update(){
SeeingPlayer = CanSeePlayer();
float Distance = Vector3.Distance(transform.position, Player.transform.position);

if ((SeeingPlayer == true)) {
transform.LookAt(Player.transform.position);

if (Distance < deathDistance){
Debug.Log("You died");
//Game over sequence starts here
}
}

if (CanHearPlayer () == true) {
Debug.Log ("I can hear you.");
}
}

protected bool CanSeePlayer()
{
RaycastHit hit;
Vector3 rayDirection = Player.transform.position - transform.position;

if ((Vector3.Angle(rayDirection, transform.forward)) <= fieldOfViewDegrees * 0.5f)
{
// Detect if player is within the field of view
if (Physics.Raycast(transform.position, rayDirection, out hit, visibilityDistance))
{
return (hit.transform.CompareTag("Player"));
}
}

return false;
}

protected bool CanHearPlayer(){

RaycastHit hit;
Vector3 rayDirection = Player.transform.position - transform.position;

if (Player.GetComponent<FirstPersonController>().MakingWalkingSound == true) {
hearDistance = 50;
} else {
hearDistance = 5;
}

if (Player.GetComponent<FirstPersonController>().MakingRunningSound == true) {
hearDistance = 100;
}

if (Physics.Raycast(transform.position, rayDirection, out hit, hearDistance))
{
return (hit.transform.CompareTag("Player"));
}
return false;
}

公共(public)游戏对象“Player”在 Unity 中定义为包含“FirstPersonController”脚本作为组件的对象。

它引用的代码(部分):

public class FirstPersonController : MonoBehaviour
{
public bool MakingWalkingSound;
public bool MakingRunningSound;

private void GetInput(out float speed)
{
// Read input
float horizontal = CrossPlatformInputManager.GetAxis("Horizontal");
float vertical = CrossPlatformInputManager.GetAxis("Vertical");
MakingWalkingSound = !(horizontal == 0 && vertical == 0);

MakingRunningSound = Input.GetKey(KeyCode.LeftShift);
}

错误读取:Assets/EnemySight.cs(53,41):错误 CS0246:找不到类型或命名空间名称“FirstPersonController”。您是否缺少 using 指令或程序集引用?并且:Assets/EnemySight.cs(59,41):错误 CS0246:找不到类型或命名空间名称“FirstPersonController”。您是否缺少 using 指令或程序集引用?

这些行对应于 CanHearPlayer 中的前两个 if 语句。

我做错了什么?我在 Google 和 StackOverflow 上进行了搜索,但找不到问题所在。

谢谢!

最佳答案

如果您的 FirstPersonController 类上有一些 namespace 声明,您需要在 EnemySight 代码上声明 using。就像:

namespace MyNamespace.Blah { 
public class FirstPersonController : MonoBehaviour {
...
}
}

和...

using MyNamespace.Blah;

public class EnemySight : MonoBehaviour {
...
}

对于 monodevelop,您可以在声明不在​​您的 using 范围内的类时使用 alt+空格键,它会为您将 using 置于该类之上。

关于c# - Unity找不到其他GameObject的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33650098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com