NullReferenceException:当从附加到游戏对象的脚本访问方法时,对象引用未设置为对象错误的实例

时间:2019-01-24 21:57:22

标签: c# unity3d

尝试从附加的脚本访问方法时,我不断收到错误align-items:flex-end

我下面的编辑器代码围成一圈创建预制件副本。我为NullReferenceException: Object reference not set to an instance of an object

放置了一个预制件

CircleSpawn

clonedObject

CircleSpawnEditor

    public class CircleSpawn : MonoBehaviour
{

    public float radius, radiusLast, spin, spinLast;
    public int numOfItems;
    //public int oldNumOfItems = 0;
    public GameObject clonedObject;
    public List<GameObject> spawnedObjects;


}

该错误发生在行

[CustomEditor(typeof(CircleSpawn))] public class CircleSpawnEditor : Editor { public override void OnInspectorGUI() { var tar = (CircleSpawn)target; tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject, typeof(GameObject), true); if (!tar.clonedObject) return; EditorGUILayout.LabelField("Radius"); // Set as required tar.radius = EditorGUILayout.Slider(tar.radius, 0f, 5f); EditorGUILayout.LabelField("Angle"); // Set as required tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f); EditorGUILayout.LabelField("Number of Items"); // Set as required tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 12); EditorGUILayout.LabelField("Object"); float angle, angleBetween = 360.0f / tar.numOfItems; if (tar.spawnedObjects == null) tar.spawnedObjects = new List<GameObject>(); if (tar.spawnedObjects.Count != tar.numOfItems) { foreach (var ob in tar.spawnedObjects) DestroyImmediate(ob); tar.spawnedObjects.Clear(); angle = 0f; for (int i = 0; i < tar.numOfItems; i++) { var rot = Quaternion.Euler(0f, 0f, tar.spin + angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedObjects.Add(Instantiate(tar.clonedObject, tar.transform.position + localPos, rot)); angle += angleBetween; tar.spawnedObjects[i].name = tar.spawnedObjects[i].name + (i + 1); var obj = GameObject.Find(tar.spawnedObjects[i].name); var pCreator = obj.GetComponent<PathCreator>(); pCreator.DrawBezierCurve(); GameObject.Find(tar.spawnedObjects[i].name).GetComponent<PCreator>().Show(); } } if (!Mathf.Approximately(tar.spin, tar.spinLast) || !Mathf.Approximately(tar.radius, tar.radiusLast)) { tar.spinLast = tar.spin; tar.radiusLast = tar.radius; angle = 0f; for (int i = 0; i < tar.numOfItems; i++) { var rot = Quaternion.Euler(0f, 0f, tar.spin + angle); var localPos = rot * Vector3.right * tar.radius; tar.spawnedObjects[i].transform.position = tar.transform.position + localPos; tar.spawnedObjects[i].transform.rotation = rot; angle += angleBetween; } } } }

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

所以在一行中的某个地方

 GameObject.Find(tar.spawnedObjects[i].name).GetComponent<PCreator>().Show();

您得到NullReferenceException吗?

这意味着在某个时候,您正在尝试使用null对象引用来做某事,就像那里有一个对象一样。

如果找不到要搜索的内容,GameObject.FindGameObject.GetComponent<T>都将返回null。因此,任何一个都可能是问题的根源。如果需要调试,请先将线分成多行:

var obj = GameObject.Find(tar.spawnedObjects[i].name);
var creator = obj.GetComponent<PCreator>();
creator.Show();

然后再次运行它,查看错误发生在哪一行。

还要尝试将整个for循环主体放在try/catch块中,并在发生错误时记录一条消息,该消息同时包含异常的Message属性和{{1}的当前值}(for循环索引变量),以便您了解问题是在哪个迭代上发生的。

做这两件事将为您提供更好的数据,以帮助您查找问题的根源。

另一件事。也许是我误会了,因为我对Unity没有太多的经验,但是看起来您正在使用i来搜索场景图并找到一个名称与GameObject.Find,您刚刚在前几行中使用了唯一的名称。您是否要查找已经引用的tar.spawnedObjects[i]?如果没有,那么这段代码的目的是什么?