- Home /
How to permanently destroy an instance?
I have 5 puzzle pieces in my game. They are instances of the same prefab. When the player walks over any of them, it gets destroyed and +1 is added to my puzzleCounter. However, when you are taken to the menu and then back to the game, the puzzles are there again (although the counter displays the number of puzzle pieces you have found).
How to permanently destroy an instance of a prefab, making sure it never spawns again until the game is restarted?
Answer by fafase · Apr 11, 2013 at 06:45 AM
When loading a scene, all variables are destroyed. So, when you finish your scene and get to the menu everything is lost. But I guess your counter is a static variable, meaning it survives destruction.
In order to "remenber" what is destroyed, you could use this static variable to know how many pieces should be created on Load. But that draws one issue, if you play the game, finish the level, close the game and start again, your puzzle is not remembered.
Best way would be to look at PlayerPrefs. You could then save the different states of your game and next time you play or when you go back to the menu, it is all remembered.
PlayerPrefs is a little bit like having a database on your local computer.
When you finish the level you use:
PlayerPrefs.SetInt("RemainingPieces", 0);
when you start the next time:
void Start(){
int pieces = PlayerPrefs.GetInt("RemainingPieces");
}
http://docs.unity3d.com/Documentation/ScriptReference/PlayerPrefs.html
But, how about their position? Do I have to name them so that I know each one's position? They are not dynamically placed.
You probably have an array of position to place them then you can use a for loop. Note that it makes it easier if you actually store the destroyed pieces:
Vector3 pos[] = new Vector3[size];
//Here you assigned the position to the array
int pieces = PlayerPrefs.GetInt("DestroyedPieces");
for (int i = size-1 ; i>pieces-1;i--){
Instantiate(prefab, pos[i],rotation)
}
That is working if you only can destroy them in order. If you are able to destroy them randomly then you would have to store the state of each I guess.
PlayerPrefs.SetInt("FirstPiece",0); // Destroyed
PlayerPrefs.SetInt("FirstPiece",1); // Not destroyed
then in the Start
Vector3 pos[] = new Vector3[size];
int [] arr = new int[size];
arr[0] = PlayerPrefs.GetInt("FirstPiece");
arr[1] = PlayerPrefs.GetInt("SecondPiece");
// and so on
//Here you assigned the position to the array
int pieces = PlayerPrefs.GetInt("DestroyedPieces");
for (int i = 0 ; i< size;i++){
if(arr[i]== 1)
Instantiate(prefab, pos[i],rotation)
}
The actual best way would be to use the UnitySerializer that allows you easily to store object information
http://whydoidoit.com/unityserializer/
But what I give there is an "easy" way to start with.
It was getting too complicated for this game (a simple assignment) so I ended up going the ghetto way, a boolean for each piece, it is set to false when collecting it, when the scene starts it checks the boolean, if false it destroys it. Quick and dirty.