简体   繁体   English

仅在 unity3d、c# 中发生的错误(使用图形)

[英]Bug that only happens in unity3d, c# (using Graphs)

So, here is the problem:所以,这里是问题:

I have a graph class (and vertex and edge) to set up a graph.我有一个图形类(以及顶点和边)来设置图形。 However, in C# (in unity3d), whenever I am adding a new vertex to my vertexList, the previous vertexes that were added get rewritten with the new one.但是,在 C# 中(在 unity3d 中),每当我向 vertexList 添加新顶点时,之前添加的顶点都会被新顶点重写。

Now, I was so confused, I even asked a university student and my computer science teacher, they were stomped, they said it themselves.现在,我很困惑,我什至问了一个大学生和我的计算机科学老师,他们被跺脚,他们自己说了。

Then, I tried to see if I wrote something wrong, so I wrote the whole thing (just the parts that help make the vertexList) in Python.然后,我试着看看我是否写错了什么,所以我用 Python 写了整件事(只是帮助制作 vertexList 的部分)。 And it worked as it should have in c# too.它也可以在 c# 中正常工作。

Below you can see the C# code (link):您可以在下面看到 C# 代码(链接):

Graph class - https://hastebin.com/usizigepaf.cs图类 - https://hastebin.com/usizigepaf.cs

Vertex class - https://hastebin.com/ofiyozodun.cs顶点类 - https://hastebin.com/ofiyozodun.cs

Edge class - https://hastebin.com/riwazawaki.cpp (this should say cs for c#, I don't know why it says cpp, it's fine anyway) Edge class - https://hastebin.com/riwazawaki.cpp(c#应该说是cs,不知道为什么说是cpp,反正没问题)

And here is the python code:这是python代码:

All - https://hastebin.com/ukokujesal.rb全部 - https://hastebin.com/ukokujesal.rb


You might find more code in the c# codes, but that is because I want to set up edges too, however, I need to set up vertexes first... that is the main problem here that I want to focus on and need help with.您可能会在 c# 代码中找到更多代码,但那是因为我也想设置边,但是,我需要先设置顶点......这是我想要关注的主要问题,需要帮助.

So, how do I solve the problem?那么,我该如何解决这个问题呢? What is going wrong?出了什么问题?


EDIT:编辑:

The problem starts when I add to the vertexList, basically on line 17 of the graph class.当我添加到 vertexList 时,问题就开始了,基本上是在图形类的第 17 行。 That line calls the method which starts at line 45, therefore, the problem can only be, in my opinion, within the method (addVertex(data)).该行调用了从第 45 行开始的方法,因此,在我看来,问题只能出在方法 (addVertex(data)) 内。

The problem clearly lies in问题显然在于

public void addVertex(Transform data)
{
    Debug.Log("data: " + data);
    //Vertex vertex = new Vertex(data);

    Vertex vertex = GetComponent<Vertex>();
    vertex.setVertex(data);
    Debug.Log("vertex: " + vertex);

    vertexList[graphIndex] = vertex;
   // Debug.Log("Type of item at graphIndex " + graphIndex + ": " + vertexList[graphIndex]);
   // Debug.Log("Data within the item at graphIndex " + graphIndex + ": " + vertexList[graphIndex].getVertex());

   graphIndex++;
}

where GetComponent<Vertex>() always returns the same instance of Vertex (the first encountered) attached to this GameObject.其中GetComponent<Vertex>()总是返回的同一实例Vertex (第一个遇到的)附连到该游戏物体。

As you figured new is forbidden for MonoBehaviour classes.正如您所认为的, MonoBehaviour类禁止使用new


Actually there is no reason why Vertex should be a MonoBehaviour at all.实际上,根本没有理由认为Vertex应该是MonoBehaviour Simply make it简单制作

[System.Serializable] // <- makes this class visible in the Inspector
                      //    including its serialized fields
public class Vertex
{
    ...

    public Vertex(Transform dataP)
    {
        data = dataP;
    }

    ...
}

so you can use new所以你可以使用new

public class Graph : MonoBehaviour
{
    [SerializedField] private Vertex[] vertexList;

    ...

    public void addVertex(Transform data)
    {
        Debug.Log("data: " + data);

        var vertex = new Vertex(data);

        Debug.Log("vertex: " + vertex);

        vertexList[graphIndex] = vertex;
       // Debug.Log("Type of item at graphIndex " + graphIndex + ": " + vertexList[graphIndex]);
       // Debug.Log("Data within the item at graphIndex " + graphIndex + ": " + vertexList[graphIndex].getVertex());

        graphIndex++;
    }

    ...
}

You might also ant to consider to use a List<Vertex> instead of the array and index if you are going to add vertices dynamically.如果您要动态添加顶点,您也可能会考虑使用List<Vertex>而不是数组和索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM