How to clear game data , with data serialization at runtime?

Hi guys, i stuck in this ,
i have a some game data which is serialized ,
in my game i have one button that clear all game data, but it is not happening what i want.
If i close the game and reopen it it is happen.

What i want is , how do i clear the data at runtime.
this below line is what i use, for button click

File.Delete (Application.persistentDataPath + "/GameData.dat");

Is there any other method to delete/clear data at runtime , on button click??

“It doesn’t work” isn’t much to go on, what’s actually the problem?

The code might not even be running, the application might not have permission to read/write etc.

I’d add a print() call next to the call to File.Delete, so you know that the code is actually running when you think it is. If your data is on disk then using System.IO is the way to go.

maybe you need be sure the files is not on stream open (being written). Make sure its file.Close() before deleting

Exactly. You could additionally test whether the file exists before and after deleting it:

string filePath = Path.Combine (Application.persistentDataPath, "GameData.dat");

Debug.LogFormat ("#BeforeDeletion - File at {0} exists: {1}", filePath, File.Exists (filePath));
File.Delete (filePath);
Debug.LogFormat ("#AfterDeletion - File at {0} exists: {1}", filePath, File.Exists (filePath));

You should probably also surround deletion with the try-catch clause to handle invalid path, file in use etc.

thx guys , i think thats the problem, the file is opened, i need to first close and then delete…
let me try…

For now what i did means,
I delete Game Data as the code above, then I delete(Destroy) the Game Data Object and then after 0.2 sec i load the loaded level.
So here the game data destroy and create New Game Data after Load Level.