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