首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问名称空间统一C#寻径中的类

访问名称空间统一C#寻径中的类
EN

Stack Overflow用户
提问于 2020-11-26 03:20:30
回答 1查看 425关注 0票数 0

我在Unity中做了一款游戏,里面有“敌人”在里面滋生(由玩家的脚本控制),我用的是敌人的预制板,我有敌人在里面,但是寻路不起作用。我知道为什么-当你引用玩家变换的时候,当敌人在场景中时,会有一个链接,但是一旦敌人是预制的,它就不能访问玩家的组件,因为它不在场景中。我做了一些研究,找到了答案-我需要像平常一样实例化敌人,但将其设置为一个变量,例如enemyGO,然后通过enemyGO.GetComponent<AIDestinationSetter>().target = gameObject访问敌人的AIDestinationSetter脚本-这将把寻路目标的target值分配给玩家(gameObject)。这一切都可以很好地工作,但它一直说它找不到AIDestinationSetter组件。经过一段时间的修补后,事实证明(我认为)这是因为在脚本中,AIDestinationSetter位于一个名称空间中。

下面是AIDestinationSetter代码:

代码语言:javascript
复制
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;
        }
    }
}

下面是我尝试访问脚本的位置:

代码语言:javascript
复制
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;

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-26 03:56:10

你可能会宁可

代码语言:javascript
复制
GetComponent<Pathfinding.AIDestinationSetter>

或拥有

代码语言:javascript
复制
using Pathfinding;

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

代码语言:javascript
复制
GetComponent<AIDestinationSetter>

您当前正在尝试为GetComponent提供一个名称空间,这显然是行不通的

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65011415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档