Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Unity中更改与对象关联的预设_C#_Unity3d - Fatal编程技术网

C# 在Unity中更改与对象关联的预设

C# 在Unity中更改与对象关联的预设,c#,unity3d,C#,Unity3d,我想更改与我的对象playerPawn关联的预设。我尝试使用以下代码执行此操作: int i = 0; PrefabUtility.DisconnectPrefabInstance(this.playerPawn); PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]); Debug.Log(this.playerPawnPrefabs[i]); PrefabUtilit

我想更改与我的对象playerPawn关联的预设。我尝试使用以下代码执行此操作:

int i = 0;

PrefabUtility.DisconnectPrefabInstance(this.playerPawn);
PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]); 

Debug.Log(this.playerPawnPrefabs[i]);

PrefabUtility.ResetToPrefabState(this.playerPawn);
但是,最后一行抛出此错误:

MissingReferenceException:已删除类型为“GameObject”的对象 已销毁,但您仍在尝试访问它。你的剧本应该 检查它是否为null,或者不应销毁该对象


Debug.Log向我显示此.playerPawnPrefacts[i]包含我期望的游戏对象,因此我不知道该错误来自何处。

也许您可以尝试将ConnectGameObjectToPrefab方法的返回重新分配给您的playerPawn对象:

this.playerPawn = PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]);

然后你可以调用ResetToPrefabState方法。

它说游戏对象被销毁了。尝试debug.log:在每次使用PrefableUtility之间设置this.playerPawn?使用debug.Logthis.playerPawn向我显示在调用ConnectGameObjectToPrefab后游戏对象被销毁。你确定this.playerPawns prefables[i]不为空吗?是的,我确定。但是,等一下,我正试图找到另一种方法来做到这一点。