在命名空间 Unity C# 寻路中访问 class

Access a class WITHIN a namespace Unity C# pathfinding

我正在 Unity 中制作一个游戏,其中 'enemies' 生成(由玩家的脚本控制),我正在使用敌人的预制件,我有敌人生成但是寻路不起作用。我知道为什么 - 这是当你在敌人在场景中时引用玩家变换时有一个 link 但是一旦敌人是预制件它就无法访问玩家的组件,因为它不在场景中。我做了一些研究并找到了答案 - 我需要像平常一样实例化敌人,但是将其设置为变量,例如 enemyGO,然后通过 [=15] 访问敌人的 AIDestinationSetter 脚本=] - 这会将寻路目标的 target 值分配给玩家 (gameObject)。这一切都很好,但它一直说找不到 AIDestinationSetter 组件。经过一段时间的修改,这被证明是(我认为)因为在脚本中,AIDestinationSetter 在命名空间内。

这是 AIDestinationSetter 代码:

using UnityEngine;
using System.Collections;

namespace Pathfinding {
    /// <summary>
    /// Sets the destination of an AI to the position of a specified object.
    /// This component should be attached to a GameObject together with a movement script such as AIPath, RichAI or AILerp.
    /// This component will then make the AI move towards the <see cref="target"/> set on this component.
    ///
    /// See: <see cref="Pathfinding.IAstarAI.destination"/>
    ///
    /// [Open online documentation to see images]
    /// </summary>
    [UniqueComponent(tag = "ai.destination")]
    [HelpURL("http://arongranberg.com/astar/docs/class_pathfinding_1_1_a_i_destination_setter.php")]
    public class AIDestinationSetter : VersionedMonoBehaviour {
        /// <summary>The object that the AI should move to</summary>
        public Transform target; // THIS IS THE VARIABLE I'M TRYING TO ACCESS
        IAstarAI ai;

        void OnEnable () {
            ai = GetComponent<IAstarAI>();
            // Update the destination right before searching for a path as well.
            // This is enough in theory, but this script will also update the destination every
            // frame as the destination is used for debugging and may be used for other things by other
            // scripts as well. So it makes sense that it is up to date every frame.
            if (ai != null) ai.onSearchPath += Update;
        }

        void OnDisable () {
            if (ai != null) ai.onSearchPath -= Update;
        }

        /// <summary>Updates the AI's destination every frame</summary>
        void Update () {
            if (target != null && ai != null) ai.destination = target.position;
        }
    }
}

这里是我尝试访问脚本的地方:

public GameObject enemyGO; // creating the empty variable

// and then further on in the script:

Vector2 spawnPos = GameObject.Find("Player").transform.position;
spawnPos += Random.insideUnitCircle.normalized * spawnRadius;
enemyGO = Instantiate(gameObject, spawnPos, Quaternion.identity);
enemyGO.GetComponent<Pathfinding>().target = spawnPos;

如果可能请帮忙,我已经尝试解决这个问题好几个小时了!谢谢

你可能更愿意

GetComponent<Pathfinding.AIDestinationSetter>

或有

using Pathfinding;

在您的脚本之上,只需使用

GetComponent<AIDestinationSetter>

您目前正在尝试为 GetComponent 提供命名空间,这显然行不通