实例化时如何从GameObject ID转到实际的GameObject



所以我正在为我的应用程序开发一个保存系统,并试图将对象存储在GameObject数组中,如下所示:

public GameObject[ ] entriesInScene;

当我在数组中存储GameObject时,它会存储GameObject ID。当我稍后尝试在代码中使用if循环实例化对象时:

foreach (GameObject go in saveObject.entriesInScene)
{
Debug.Log(go);
GameObject entry = Instantiate(go, position, Quaternion.identity);
entry.transform.SetParent(null);
entry.transform.SetParent(scroll.transform);
entry.transform.localScale = new Vector3(1,1,1);
}

它给了我一个MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.的错误

有人知道如何解决这个问题吗?或者知道如何从ID中获取GameObject以使实例化工作吗?

据我所知,通过Unity,您将无法直接通过序列化保存整个GameObjects。对于实现保存系统的类似问题,我的方法是将每个对象类型存储为预制件,然后将重要值本身保存在脚本中(例如,用户可以放置微波预制件,但我会为该位置存储Vector3(。在大多数情况下,您应该能够只存储预制实例之间更改的值。

最新更新