Search Unity

Deleting A Single GameObject From PlayerPref

Discussion in 'Scripting' started by SiMULOiD, Jun 7, 2021.

  1. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Hi all,
    I'm using this code to save an object's transforms to a world map manager script.
    What I'm trying to understand is how to delete the same object from PlayerPref after it has been instantiated.

    Code (CSharp):
    1.  
    2.  
    3. public void InstatiateAndSaveToWorldMap()
    4.     {
    5.         string tagText = TagInputField.text.Trim();
    6.         GameObject new3DTag = Instantiate3DTag(tagText, Camera.main.transform.position);
    7.  
    8.         int updatedTagCounts = Tag3DParentTransform.childCount;
    9.         PlayerPrefs.SetInt("PlayerPrefsTagsCountKey", updatedTagCounts);
    10.         PlayerPrefs.SetString("PlayerPrefsTag_" + updatedTagCounts, tagText + "," + new3DTag.transform.position.x + "," + new3DTag.transform.position.y + "," + new3DTag.transform.position.z);
    11.         PlayerPrefs.Save();
    12.  
    13.         _worldMapManager.Save();
    14.  
    15.         TagInputField.text = "";
    16.  
    17.     }

    I think I need to do something like:
    PlayerPrefs.SetInt("PlayerPrefsTagsCountKey", -1);
    But this would only reduce the count, not the transform of the "new3DTag" game object.
     
    Last edited: Jun 7, 2021
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can delete a key as @Antistone says, but I feel like we'd be doing you a disservice if we failed to recommend that you ditch PlayerPrefs. It has its uses, but the instant you try to squeeze any kind of data structure into it, you're just giving yourself more headaches. Search up any tutorial for saving your game with JSON - once it's set up (and it's not too difficult to set up) it is MUCH easier than trying to make PlayerPrefs something it was sort of designed NOT to do.
     
    PraetorBlue likes this.
  4. SiMULOiD

    SiMULOiD

    Joined:
    Dec 23, 2013
    Posts:
    126
    Thank you both. I appreciate the direction and will look into using JSON for saving.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    That advice seems a little weird to me, because I think of PlayerPrefs as a location for saving data, and JSON as a format. You can shove JSON into PlayerPrefs if you want, and you can also create your own save files using formats other than JSON.

    But it's true that a robust understanding of serialization techniques (like JSON) will probably make saving complex game data much easier for you, and that PlayerPrefs isn't a particularly natural place to store saved games.
     
    Bunny83 likes this.
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    We're definitely nitpicking semantics here, but PlayerPrefs is both location and format (and really, the fact that it's both at once is its main appeal to rookie Unity devs). Like, yes you could shove JSON into PlayerPrefs.SetString, but you could also shove a JSON string into a string field in another JSON; that doesn't make the "outer" JSON any less a format.
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    *shrug* You could also look at your computer's file system as an "outer format" that stores data as key/value pairs where the "keys" are file paths, and the "true" location is the physical media on your computer. OP is already doing some sort of basic custom serialization of a Vector3 and stuffing the result into PlayerPrefs.SetString.

    I guess my main point is that in order to avoid using PlayerPrefs, you're going to need to learn at least one non-PlayerPrefs location for saving data into, and "JSON" does not in itself give you that. (Though I imagine there are a great many tutorials on the topic of "saving games in Unity using JSON" that probably cover it.)