Unity无法找到其他GameObject的脚本

时间:2015-11-11 11:59:16

标签: c# unity3d

我从另一个GameObject的脚本中获取变量时遇到问题..我之前使用过这种类型的引用,我知道它是如何工作的,但由于某种原因,它说它无法找到我指的是脚本。

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

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;
}

}

公共GameObject&#39;播放器&#39;在Unity中定义为包含&#39; FirstPersonController&#39;脚本作为一个组件。

它所指的代码(部分代码):

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:类型或命名空间名称&#39; FirstPersonController&#39;无法找到。您是否缺少using指令或程序集引用? 并且:Assets / EnemySight.cs(59,41):错误CS0246:类型或命名空间名称&#39; FirstPersonController&#39;无法找到。您是否缺少using指令或程序集引用?

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

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

谢谢!

1 个答案:

答案 0 :(得分:5)

如果您的FirstPersonController类上有一些名称空间声明,则需要在EnemySight代码上声明使用。就像:

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

和...

using MyNamespace.Blah;

public class EnemySight : MonoBehaviour {
    ...
}

对于monodevelop,您可以在声明尚未在您的使用范围内的类时使用alt +空格键,并且它将在您的课程顶部使用。