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

问题描述

NullReferenceException: Object reference not set to an instance of an object尝试从附加脚本访问方法时,我不断收到错误消息。

我下面的编辑器代码在一个圆圈中创建预制副本。我为clonedObject

圈子生成

    public class CircleSpawn : MonoBehaviour
{

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


}

CircleSpawn编辑器

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

            }
        }
    }
}

错误发生在该行

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

我该如何解决这个问题?

标签: c#unity3d

解决方案


所以在某处

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

你得到一个NullReferenceException

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

如果没有找到要搜索的东西,两者都GameObject.Find将返回 null。GameObject.GetComponent<T>因此,任何一个都可能是您问题的根源。如果我需要对此进行调试,我会先将该行分成多行:

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

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

另外,尝试将整个 for 循环主体放在一个try/catch块中,当发生错误时,记录一条消息,其中包含异常的Message属性和i(for 循环索引变量)的当前值,以便您知道问题出在哪个迭代上发生。

做这两件事会给你更好的数据来帮助你追踪问题的根源。

另一件事。也许我误解了,因为我没有大量的 Unity 经验,但看起来你正在使用GameObject.Find搜索场景图并找到一个与 同名的对象tar.spawnedObjects[i],你刚刚在前面几行中设置了一个唯一的名称。您是否正在尝试查找tar.spawnedObjects[i]您已经参考过的 ?如果不是,这段代码的目的是什么?


推荐阅读