Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么航路点列表和代理列表为空?_C#_Unity3d - Fatal编程技术网

C# 为什么航路点列表和代理列表为空?

C# 为什么航路点列表和代理列表为空?,c#,unity3d,C#,Unity3d,我创建了WaypointsClass类: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using System; using UnityEngine.AI; [Serializable] public class WaypointsClass { public List<Transform> points = new Lis

我创建了WaypointsClass类:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System;
using UnityEngine.AI;

[Serializable]
public class WaypointsClass
{
    public List<Transform> points = new List<Transform>();
    public List<NavMeshAgent> agents = new List<NavMeshAgent>();
}
蓝色:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class BlueWaypoints : MonoBehaviour
{
    public GameObject[] blueWaypoints;
    public NavMeshAgent blueAgent;

    // Start is called before the first frame update
    void Start()
    {
        blueWaypoints = GameObject.FindGameObjectsWithTag("Blue_Waypoint");

        WaypointsClass wpc = new WaypointsClass();

        foreach (GameObject point in blueWaypoints)
        {
            wpc.points.Add(point.transform);
        }

        wpc.agents.Add(blueAgent);
    }

    // Update is called once per frame
    void Update()
    {

    }
}
然后,我在层次结构和场景组中创建了红色航路点和红色字符以及蓝色字符的蓝色航路点组:

我想做的主要目标是使用相同的WaypointsAI脚本在蓝色航路点之间移动蓝色字符,在红色航路点之间移动红色字符。如果蓝色组有5个字符,则在蓝色航路点之间移动5个字符;如果红色组有50个字符,则在红色航路点之间移动5个字符

目前的第一个问题是WaypointsAI脚本中列出的点和代理是空的。 我在这行中添加了一个断点:

if (waypoints.points.Count < 1)
if(航路点.points.Count<1)
两个列表都是空的

我尝试将脚本RedWaypoints和BlueWaypoints中Start()中的代码移动到Awake()中,它为WaypointsClass类创建了一个新实例,但当它到达WaypointsAI脚本时,列表为空。我不明白为什么

在这个截图中,我有两个字符,一个是T型,一个是蓝色的圆环。我希望每一个都能在其他航路点之间移动。一个可以爬楼梯,另一个可以移到窗户,然后再移回来:


您的问题:您无法访问在其他脚本中创建的类的实例

如果我了解你的问题。。。我建议您使用静态类和静态列表,以便可以从任意脚本访问:

public static class Waypoints
{
    public static List<Transform> Redpoints = new List<Transform>();
    public static List<NavMeshAgent> Redagents = new List<NavMeshAgent>();
    public static List<Transform> Bluepoints = new List<Transform>();
    public static List<NavMeshAgent> Blueagents = new List<NavMeshAgent>();
}

从另一个脚本中,如果您想访问第一个红点项目,只需键入
Waypoints.Redpoints[0]
,对于第一个红色代理,只需键入
Waypoints.Redagents[0]
,对于蓝色面也是一样


在航路点SAI中,您不需要声明
公共航路点CLASS航路点

我不明白,您有一份红色和蓝色的积分和代理列表,还是每组都有自己的积分和代理列表?@Frenchy每组都有自己的积分和代理列表。主要目标思想:我想让多个角色在不同的航路点之间移动。例如,我添加了20个标记为“航路点”的立方体,我有3个字符,我希望每个字符在不同的航路点之间移动。例如,角色1将在立方体1到5之间移动,角色2在3到10之间,角色3在11到20之间,确定你想要的武器,可以到达红色和蓝色的代理和点?对。每个红色或蓝色脚本可以有多个代理和航路点。可能是蓝色的一个航路点,有5个字符,将在字符原始位置和航路点之间移动,红色的有10个字符和30个航路点,红色的字符将在30个红色航路点之间移动。@Frenchy在链接中的这个屏幕截图中更简单,我有两个字符,一个是t姿势,另一个是带有蓝色的圆圈在中间。我希望每一个都能在其他航路点之间移动。一个可以爬楼梯,另一个可以移到窗户,然后再移回来。imgur.com/a/faywwr
if (waypoints.points.Count < 1)
public static class Waypoints
{
    public static List<Transform> Redpoints = new List<Transform>();
    public static List<NavMeshAgent> Redagents = new List<NavMeshAgent>();
    public static List<Transform> Bluepoints = new List<Transform>();
    public static List<NavMeshAgent> Blueagents = new List<NavMeshAgent>();
}
public class RedWaypoints : MonoBehaviour
{
    public GameObject[] redWaypoints;
    public NavMeshAgent redAgent;

    // Start is called before the first frame update
    void Start()
    {
        redWaypoints = GameObject.FindGameObjectsWithTag("Red_Waypoint");

        foreach (GameObject point in redWaypoints)
        {
            Waypoints.Redpoints.Add(point.transform);
        }

        Waypoints.Redagents.Add(redAgent);
    }
    :
    :
}
public class BlueWaypoints : MonoBehaviour
{
    public GameObject[] blueWaypoints;
    public NavMeshAgent blueAgent;

    // Start is called before the first frame update
    void Start()
    {
        blueWaypoints = GameObject.FindGameObjectsWithTag("Blue_Waypoint");


        foreach (GameObject point in blueWaypoints)
        {
            Waypoints.Bluepoints.Add(point.transform);
        }

        Waypoints.Blueagents.Add(blueAgent);
    }
    :
    :
}