Reputation:
I want to change the prefab my object playerPawn
is associated with. 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. Your script should either check if it is null or you should not destroy the object.
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.
Upvotes: 0
Views: 637
Reputation: 43
Maybe you can try to reassign the return of ConnectGameObjectToPrefab method to your playerPawn object:
this.playerPawn = PrefabUtility.ConnectGameObjectToPrefab (this.playerPawn, this.playerPawnPrefabs [i]);
Then you can call ResetToPrefabState method.
Upvotes: 1