为什么航点列表和代理列表是空的?

问题描述 投票:0回答:1

我创建了一个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>();
}

然后创建了WaypointsAI脚本。

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

public class WaypointsAI : MonoBehaviour
{
    public WaypointsClass waypoints;
    public float distanceToContinue;

    private float currentDistanceToPoint;
    private int lastPointIndex;
    private int goalPointIndex;


    void Start()
    {
        //Firstly check if the waypoints are set up correctly
        if (waypoints.points.Count < 1)
        {
            Debug.LogError("Set up the waypoints for this gameObject!");
        }
        else
        {
            //Now set up the path
            lastPointIndex = 0; //Start from the index 0
            waypoints.agents[0].transform.position = waypoints.points[0].position;
            if (waypoints.points.Count > 1)
            {
                goalPointIndex = 1; //Go to the [1] waypoint
            }
            else
            {
                goalPointIndex = 0;
            }
        }
    }

    void FixedUpdate()
    {
        for (int i = 0; i < waypoints.agents.Count; i++)
        {
            //Calculate the distance and check if it should move to the next waypoint.
            currentDistanceToPoint = Vector3.Distance(waypoints.agents[i].transform.position, waypoints.points[goalPointIndex].position);
            if (currentDistanceToPoint <= distanceToContinue)
            {
                //Save the old index, totally useless in this implementation though
                lastPointIndex = goalPointIndex;
                //Increase goal index to change the goal waypoint to the next, (Or maybe random one?)
                goalPointIndex++;
                if (goalPointIndex >= waypoints.points.Count)
                    goalPointIndex = 0;
            }

            //Now move towards the current waypoint, Change this to fit your code with navMesh anyway I think I did a lot for you anyway
            waypoints.agents[i].transform.LookAt(waypoints.points[goalPointIndex].position);
            waypoints.agents[i].transform.Translate(Vector3.forward * Time.deltaTime * 10);
        }
    }
}

然后为每组代理和航点添加一个脚本。红色和蓝色。

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

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");

        WaypointsClass wpc = new WaypointsClass();

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

        wpc.agents.Add(redAgent);
    }

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

    }
}

和蓝色。

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()
    {

    }
}

然后,我在Hierarchy和Scene中创建了一组红色航点和红色角色以及一组蓝色航点和蓝色角色。

Waypoints

我想做的主要目标是在蓝色航点之间移动蓝色角色,并使用相同的WaypointsAI脚本在红色航点之间移动红色角色。如果蓝色组有5个角色,那么就在蓝色航点之间移动这5个角色,如果红色组有50个角色,则在红色航点之间移动。

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

if (waypoints.points.Count < 1)

而且两个列表都是空的。

我试着把RedWaypoints和BlueWaypoints两个脚本中Start()中的代码移到Awake()中,它为WaypointsClass类做了一个新的实例,但是当它进入WaypointsAI脚本时,列表是空的。我想不通为什么。

更简单的是,在这张截图中,我有两个角色,一个是T姿势的角色,另一个是中间有蓝色圆圈的机器人,我希望每个角色都能在其他航点之间移动。

Simpler

c# unity3d
1个回答
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);
    }
    :
    :
}

从另一个脚本中,如果你想访问第一个红点项目,你只需要输入 Waypoints.Redpoints[0] 而对于你的 "第一红 "代理,你只需输入 Waypoints.Redagents[0] 蓝方也是如此

在WaypointsAI中你不需要声明 public WaypointsClass waypoints;

© www.soinside.com 2019 - 2024. All rights reserved.