【问题标题】:My list is not updating in one of my scripts but is in the other我的列表没有在我的一个脚本中更新,但在另一个
【发布时间】:2018-01-03 17:59:37
【问题描述】:

我的第一个脚本是创建一个字符串列表,并在每次我的玩家或名为 pacman_1 的游戏对象之间发生冲突时更新。这是代码:

public class Node : MonoBehaviour
{
    public List<string> FinalPath = new List<string>();
    public static GameObject PlayerPos, EnemyPos;
    public static string SPlayerPos, SEnemyPos;
    public static bool E, P;
    // Use this for initialization

    void Start()
    {
        var box = gameObject.AddComponent<BoxCollider2D>();
        box.isTrigger = enabled;


    private void Update()
    {

    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (other.name == "pacman_1")
        {   
            SEnemyPos = EnemyPos.name
            E = true;

        }

        if (other.name == "player")
        {

            SPlayerPos = PlayerPos.name;
            P = true;
        }


        if (P == true || E == true)
        {

            Graph g = new Graph();
            g.add_vertex("P1", new Dictionary<string, int>() { { "P41", 3 }, { "P49", 2 } });

            /***********************************/
            /**** 62 vertices omitted -- ed ****/
            /***********************************/

            g.add_vertex("P64", new Dictionary<string, int>() { { "P34", 3 }, { "P44", 2 } });

            g.shortest_path(SPlayerPos, SEnemyPos).ForEach(x => FinalPath.Add(x));

            E = false;
            P = false;

        }
    }
}

public class Graph
{
    Dictionary<string, Dictionary<string, int>> vertices = new Dictionary<string, Dictionary<string, int>>();
    public void add_vertex(string name, Dictionary<string, int> edges)
    {
        vertices[name] = edges;
    }
    public List<string> shortest_path(string start, string finish)
    {
        var previous = new Dictionary<string, string>();
        var distances = new Dictionary<string, int>();
        var nodes = new List<string>(); 
        List<string> path = null;
        foreach (var vertex in vertices)
        {
            if (vertex.Key == start)
            {
                distances[vertex.Key] = 0;
            }
            else
            {
                distances[vertex.Key] = int.MaxValue;
            }

            nodes.Add(vertex.Key);
        }

        while (nodes.Count != 0)
        {
            nodes.Sort((x, y) => distances[x] - distances[y]);

            var smallest = nodes[0];
            nodes.Remove(smallest);

            if (smallest == finish)
            {
                path = new List<string>();
                while (previous.ContainsKey(smallest))
                {
                    path.Add(smallest);
                    smallest = previous[smallest];
                }

                break;
            }

            if (distances[smallest] == int.MaxValue)
            {
                break;
            }

            foreach (var neighbor in vertices[smallest])
            {
                var alt = distances[smallest] + neighbor.Value;
                if (alt < distances[neighbor.Key])
                {
                    distances[neighbor.Key] = alt;
                    previous[neighbor.Key] = smallest;
                }
            }
        }
        path.Add(start);
        return path;
    }
}

那么这是我第一次正确访问列表但之后保持不变的另一个脚本:

public class enemy : MonoBehaviour
{
    List<GameObject> finP = new List<GameObject>();
    public Node nodeScript;
    public float Speed = 1.0f;
    // Use this for initialization
    void Start()
    {
        nodeScript = GameObject.Find("P16").GetComponent<Node>();

    }

    // Update is called once per frame
    void Update()
    {
        MovePosition();
    }

    public void OnTriggerEnter2D(Collider2D other)
    {

        if (other.gameObject.tag == "pallet" && nodeScript.FinalPath.Count>1)
        {

            finP.Clear();
            foreach(string var in nodeScript.FinalPath)
            {
                finP.Add(GameObject.Find(var));
            }
        }
    }

    void MovePosition()
    {
        if (finP.Count < 1) return;
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, finP[1].transform.localPosition, Speed * Time.deltaTime);
    }
}

这是在我的控制台中按预期输出的原始列表: screen shot of console

那么这是在一个脚本中更新的列表,而不是在另一个脚本中:

another screen shot

【问题讨论】:

  • 你能删除所有不必要的代码吗?如果您删除了所有注释掉的代码和额外的空白,那么看起来就不那么令人生畏了。
  • @EdPlunkett 没关系,但我看到用户也因为留下注释掉的代码而受到殴打。
  • @Eddge 确实是这样,这也是我在 cmets 中提出这一点的部分原因。
  • 现在不行了,抱歉保留注释的代码,这样我就可以向老师展示我的代码随着时间的推移是如何变化的
  • @EdPlunkett 是的,你之前曾试图帮助我。我只是在询问一般的想法,例如这种情况通常发生在哪里,但由于需要代码而重新发布

标签: c# list unity3d


【解决方案1】:

是您要更新的List&lt;string&gt; FinalPath 吗?如果是这样,请尝试在 Start() 函数中初始化它,如下所示:

void Start(){

      FinalPath = new List<string>();
}

我不确定,但我认为列表不能在方法之外初始化。

【讨论】:

  • 感谢您的建议,它只是给了我相同的输出。
【解决方案2】:

我会查看一些区域,看看它们是否与脚本的预期结果相冲突。

nodeScript = GameObject.Find("P16").GetComponent<Node>();

这是在GameObject 上获取名为“P16”的Node 组件,因此nodeScript.FinalPath 调用将仅引用该节点。这是故意的吗?会不会是“P16”节点只接收到一定数量的冲突。

nodeScript.FinalPath.Count>1

当列表中至少有两个元素时返回true;您是否打算进行&gt; 0 操作?

foreach(string var in nodeScript.FinalPath)
{
    finP.Add(GameObject.Find(var));
}

var 是 C# 中的关键字,我会避免将其用作变量名。虽然它似乎可以编译。

【讨论】:

  • 我认为这一定是因为它只有在敌人与用于链接两个脚本(在本例中为 P16)的游戏对象发生碰撞时才有效,因为我将其更改为“P1 " 并且在敌人脚本中没有访问最终路径。
  • nodeScript .FinalPath.count
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-21
  • 2021-02-11
  • 2018-02-13
相关资源
最近更新 更多