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

时间:2019-04-03 02:07:47

标签: c# class unity3d

我有一个问题,我有一个传递给函数的游戏对象列表。即时将游戏对象放在列表中的第I个位置,并访问放置在游戏对象上的脚本的公共变量。 var应该返回一个游戏对象,但返回null。这就是我的代码的工作方式,

  1. 脚本levelMechanics使用脚本nodeMechanics创建游戏对象网格
  2. levelMechanics调用脚本roomSpawner,该脚本告诉每个节点在其中运行nodeMechanics代码,该代码获取节点周围的节点并将其存储在公共变量中
这两个步骤都很好,我可以单击每个节点,在层次结构中,附加到每个节点的nodeMechanics脚本将告诉我当前节点的上方,下方,左侧和右侧的节点。问题是,如果我尝试访问节点周围的节点,则会得到空值。香港专业教育学院尝试过的事情,

  1. 通过访问levelMechanics而不是levelMechanics将其放入roomSpawner,从而在roomSpawner中获取列表。

  2. 传入列表并进行复制并访问复制的列表。

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

  4. 获取节点的nodeMechanics,进行存储,然后尝试获取已存储的nodeMechanics的数据,而不是直接跳入获取数据。

im刚刚丢失,因为层次结构说这些var不为null,但是当我尝试用代码访问它们时,它们就是null。以下代码是我尝试访问vars

  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

//这是levelMechanics脚本

   public List<GameObject> objects;
    public List<GameObject> nodes;
    public GameObject floor;
    public float offX, offZ;
    void Awake()
    {
        RoomCheck();
        RoomSetup();
        //GetComponent<roomSpawner>().SpawnEnemys(nodes);
        this.GetComponent<roomSpawner>().IntializeNodes(nodes);
        this.GetComponent<roomSpawner>().SpawnObjects(nodes);


    }
void RoomSetup()
    {
        bool first = true;
        int collCount = -1;
        GameObject placeHolder = new GameObject("temp");
        float colls;

        colls = floor.transform.localScale.x - 1;

        offX = floor.transform.localScale.x;
        offZ = floor.transform.localScale.z;
        offX = (offX / 2) - offX + 1;
        offZ = (offZ / 2) - 1;
        offX = offX + floor.transform.position.x;
        offZ = (offZ + floor.transform.position.z);
        //print(colls);
        for (int i = 0; i < ((floor.transform.localScale.x-1) * (floor.transform.localScale.z-1)); i++)
        {
            collCount++;
            //print(collCount);
            GameObject temp = new GameObject(i + " Node");
            temp.transform.SetParent(floor.transform);
            temp.AddComponent<BoxCollider>();
            temp.GetComponent<BoxCollider>().isTrigger = true;
            temp.AddComponent<nodeMechanics>();
            temp.layer = 9;

            if(!first)
            {
                temp.transform.position = new Vector3((placeHolder.transform.position.x + 1), 6, placeHolder.transform.position.z);
                placeHolder = temp;
                if (collCount >= colls)
                {
                    //print("new line on " + temp.name + " coll " + collCount);
                    collCount = 0;
                    temp.transform.position = new Vector3(floor.transform.localPosition.x + offX, 6, placeHolder.transform.position.z - 1);
                    placeHolder = temp;
                }
            }
            if (first)
            {
                // print(colls);
                temp.transform.position = new Vector3(floor.transform.localPosition.x+offX, 6, floor.transform.localPosition.z+offZ);
                placeHolder = temp;
                first = false;
            }

            nodes.Add(temp);
        }

    }

这是nodeMechanics脚本

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

temp.nodeDown应该返回正确的节点,但返回null,在层次结构中它显示正确的对象,而不是null

1 个答案:

答案 0 :(得分:0)

解决了,因为我是在intializeNodes之后立即调用spawnRoom的,我想我已经团结起来将它们彼此叠加了