【问题标题】:Access a class WITHIN a namespace Unity C# pathfinding访问命名空间内的类 Unity C# 寻路
【发布时间】:2020-11-25 19:20:29
【问题描述】:

我正在 Unity 中制作一个游戏,其中“敌人”生成(由玩家的脚本控制),我正在使用敌人的预制件,我已经让敌人生成,但寻路不是在职的。我知道为什么 - 这是当你在敌人在场景中时引用玩家变换时,会有一个链接,但是一旦敌人是预制件,它就无法访问玩家的组件,因为它不在场景中。我做了一些研究并找到了答案——我需要像平常一样实例化敌人,但是将它设置为一个变量,例如enemyGO,然后通过enemyGO.GetComponent<AIDestinationSetter>().target = gameObject访问敌人的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;

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

【问题讨论】:

  • 您能提供您所看到的错误的详细信息吗?

标签: c# class unity3d path-finding a-star


【解决方案1】:

你可能更愿意

GetComponent<Pathfinding.AIDestinationSetter>

或者有

using Pathfinding;

在您的脚本之上并简单地使用

GetComponent<AIDestinationSetter>

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

【讨论】:

  • 谢谢!!问题解决了,万分感谢!!!
猜你喜欢
  • 2010-11-22
  • 2017-01-24
  • 2012-08-06
  • 2020-09-29
  • 2013-08-30
  • 2020-02-25
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多