[英]Changing the prefab an object is associated with in Unity
I want to change the prefab my object playerPawn
is associated with. 我想更改与对象
playerPawn
关联的预制件。 I tried to do it with this code: 我尝试使用以下代码进行操作:
int i = 0;
PrefabUtility.DisconnectPrefabInstance(this.playerPawn);
PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]);
Debug.Log(this.playerPawnPrefabs[i]);
PrefabUtility.ResetToPrefabState(this.playerPawn);
However, the last line is throwing this error: 但是,最后一行抛出此错误:
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
MissingReferenceException:'GameObject'类型的对象已被破坏,但您仍在尝试访问它。 Your script should either check if it is null or you should not destroy the object.
您的脚本应检查其是否为null或不破坏该对象。
The Debug.Log()
showed me that this.playerPawnPrefabs[i]
contains the GameObject I'm expecting, so I don't understand where that error comes from. Debug.Log()
向我展示了this.playerPawnPrefabs[i]
包含了我期望的this.playerPawnPrefabs[i]
,所以我不明白该错误的this.playerPawnPrefabs[i]
。
Maybe you can try to reassign the return of ConnectGameObjectToPrefab method to your playerPawn object: 也许您可以尝试将ConnectGameObjectToPrefab方法的返回值重新分配给playerPawn对象:
this.playerPawn = PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]);
Then you can call ResetToPrefabState method. 然后,您可以调用ResetToPrefabState方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.