Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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# Getcomponet.var返回null,但public var不为null_C#_Class_Unity3d - Fatal编程技术网

C# Getcomponet.var返回null,但public var不为null

C# Getcomponet.var返回null,但public var不为null,c#,class,unity3d,C#,Class,Unity3d,我有一个问题,我有一个游戏对象列表被传递到一个函数中。我在列表中的第i个位置获取游戏对象,并获取放置在游戏对象上的脚本的公共变量。var应该返回一个gameobject,但返回null。这就是我的代码的工作原理 脚本级别机制使用脚本节点机制创建游戏对象网格 levelMechanics调用脚本roomSpawner,它告诉每个节点在那里运行nodeMechanics代码,该代码获取节点周围的节点并将它们存储在公共变量中 这两个步骤都非常有效,我可以单击每个节点,在层次结构中,附加到每个节点的no

我有一个问题,我有一个游戏对象列表被传递到一个函数中。我在列表中的第i个位置获取游戏对象,并获取放置在游戏对象上的脚本的公共变量。var应该返回一个gameobject,但返回null。这就是我的代码的工作原理

  • 脚本级别机制使用脚本节点机制创建游戏对象网格
  • levelMechanics调用脚本roomSpawner,它告诉每个节点在那里运行nodeMechanics代码,该代码获取节点周围的节点并将它们存储在公共变量中
  • 这两个步骤都非常有效,我可以单击每个节点,在层次结构中,附加到每个节点的nodeMechanics脚本将告诉我当前节点的上、下、左、右节点。问题是,如果我尝试访问节点周围的节点,我会得到null。我试过的东西

  • 通过levelMechanics而不是levelMechanics将列表放入roomSpawner,在roomSpawner中获取列表

  • 传入列表并复制它,然后访问复制的列表

  • 列表[i].getcomponet(nodemechanics).nodeDown和列表[i].gameobject.getcomponet(nodemechanics).nodeDown

  • 获取节点的节点机制,存储它,然后尝试获取存储的节点机制的数据,而不是直接获取数据
  • 我只是迷路了,因为层次结构说这些变量不是空的,但当我试图在代码中访问它们时,它们是空的。下面的代码是我尝试访问VAR的代码

      public GameObject test;
      public nodeMechanics temp;
    
    public void IntializeNodes(List<GameObject> nodes)
        {
            for (int i = 0; i < nodes.Count; i++)
            {
                nodes[i].gameObject.GetComponent<nodeMechanics>().test = true;
    //im not sure why i am able to write but not read
    
            }
    
        }
        public void SpawnObjects(List<GameObject> nodes)
        {
            //this function is being called in level mechanics and the list is being passed in
            print("nodes created");
            for (int i = 0; i < nodes.Count; i++)
            {
                noders.Add(nodes[i]);//the current method is copy over the list and try to access the copy but that doesnt work. i tried not copying
                test = noders[i].gameObject;//ive tried not splint this up and jumping straight into nodes[i]..GetComponent<nodeMechanics>().nodeDown;
                temp = test.GetComponent<nodeMechanics>();
                print(temp.nodeDown);//no matter what i do this always returns null. but in the hierarchy it is not
    
    为澄清以下代码工作不正常

     for (int i = 0; i < nodes.Count; i++)
            {
                noders.Add(nodes[i]);//the current method is copy over the list and try to access the copy but that doesnt work. i tried not copying
                test = noders[i].gameObject;//ive tried not splint this up and jumping straight into nodes[i]..GetComponent<nodeMechanics>().nodeDown;
                temp = test.GetComponent<nodeMechanics>();
                print(temp.nodeDown);
    
    for(int i=0;i

    temp.nodeDown应该返回正确的节点,但返回null,在层次结构中,它显示正确的对象,而不是null已解决,因为我在初始化节点之后调用了产卵室,我想unity会在彼此的顶部运行它们

    ,这对我来说是大量的文本和代码,没有更明确的指导来阅读哪行code有问题。当然,我为Ya编辑了如果您使用
    var bob
    而不是
    temp
    (然后在下一行使用
    bob.nodeDown
    ),会发生什么情况?这样行吗?仍然返回null刚刚添加的函数GameObject getDown(){return nodeDown;}到节点机制并调用它,我试图打印出nodeDown,但仍然得到null
    public class nodeMechanics : MonoBehaviour
    {
        public GameObject nodeUp, nodeDown, nodeLeft, nodeRight, forced;
        public bool test = false;
        void FixedUpdate()
        {
            if (test)
            {
                FindAround();
            }
            if(forced != null)
            {
                Instantiate(forced, this.transform.position, forced.transform.rotation);
            }
        }
    
        public void FindAround()
        {
            int mask = LayerMask.GetMask("Node");
            RaycastHit up, down, left, right;
            if (Physics.Raycast(this.transform.position, Vector3.forward, out up, 1, mask))
            {
                nodeUp = up.collider.transform.gameObject;
            }
            if (Physics.Raycast(this.transform.position, Vector3.back, out down, 1, mask))
            {
                nodeDown = down.collider.transform.gameObject;
            }
            if (Physics.Raycast(this.transform.position, Vector3.left, out left, 1, mask))
            {
                nodeLeft = left.collider.transform.gameObject;
            }
            if (Physics.Raycast(this.transform.position, Vector3.right, out right, 1, mask))
            {
                nodeRight = right.collider.transform.gameObject;
            }
        }
    }
    
    
     for (int i = 0; i < nodes.Count; i++)
            {
                noders.Add(nodes[i]);//the current method is copy over the list and try to access the copy but that doesnt work. i tried not copying
                test = noders[i].gameObject;//ive tried not splint this up and jumping straight into nodes[i]..GetComponent<nodeMechanics>().nodeDown;
                temp = test.GetComponent<nodeMechanics>();
                print(temp.nodeDown);