C# 将游戏对象类型列表添加到游戏对象列表时获取null

C# 将游戏对象类型列表添加到游戏对象列表时获取null,c#,unity3d,C#,Unity3d,主要目标是找到包含脚本GenerateGuid的所有对象,并将它们添加到列表objectsToSave中。首先尝试初始化列表 objectsToSave.Add(objectsWithGenerateGuid[i].gameObject); private List objectsToSave=new List(); 由于countobjectsWithGenerateGuid.count大于0,因此我假定objectsToSave为null,这就是为什么会出现null指针异常 using

主要目标是找到包含脚本GenerateGuid的所有对象,并将它们添加到列表objectsToSave中。

首先尝试初始化列表

objectsToSave.Add(objectsWithGenerateGuid[i].gameObject);
private List objectsToSave=new List();
由于count
objectsWithGenerateGuid.count
大于0,因此我假定
objectsToSave
null
,这就是为什么会出现null指针异常

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class GenerateAutomaticGuid : Editor
{
    [MenuItem("GameObject/Generate Guid", false, 11)]
    private static void GenerateGuid()
    {
        foreach (GameObject o in Selection.gameObjects)
        {
            var g = o.GetComponent<GenerateGuid>();
            if (!g) g = o.AddComponent<GenerateGuid>();
            g.GenerateGuidNum();
        }
    }
}
objectsToSave.Add(objectsWithGenerateGuid[i].gameObject);
private List<GameObject> objectsToSave = new List<GameObject>();