Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

DirectoryInfo help

Discussion in 'Scripting' started by Cyrussphere, Oct 6, 2016.

  1. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    Hi all,

    Had an issue I ran into and was hoping someone could offer some advise. I am working on a Current Saves menu which has the option to either Load or Delete the save. After struggling with lists for a while I came across DirectoryInfo[] I have all player games saved within their own folders so instead of trying to find a single save file I wanted to get the entire directory name. So far my code is finding the directory correctly and listing the name correctly, also the Delete is successfully clearing out the proper directory and all its contents. The issue is trying to clear the DirectoryInfo array of any deleted games. The array clings onto the saved games when the game started and any new ones created but it wont remove any deleted games from the list. Here is the current code I am trying;

    Code (CSharp):
    1.     public GameObject gameButton;
    2.     public GameObject ListFormatter;
    3.     static DirectoryInfo dir = new DirectoryInfo(Application.persistentDataPath + "/");
    4.     static DirectoryInfo[] info;
    5.  
    6.     //Get fresh list of saved games when panel is activated
    7.     void OnEnable()
    8.     {    
    9.         info = dir.GetDirectories();
    10.  
    11.         foreach (DirectoryInfo f in info)
    12.         {
    13.             var newButton = Instantiate(gameButton);
    14.             newButton.GetComponentInChildren<Text>().text = f.Name;
    15.             newButton.transform.SetParent(ListFormatter.transform, false);
    16.         }    
    17.     }
    18.     //Clear array when panel is deactivated so we can start fresh when panel is activated again
    19.     void OnDisable()
    20.     {
    21.         info = null;
    22.     }
    In the above I'm trying to set the array to null, I have also tried Array.Clear(info, 0, info.Length); but it didnt seem to do anything either.

    Any ideas on how I could do this? I thought about trying to convert to a list but I am getting errors trying to convert DirectoryInfo data into a list.

    Thanks!
     
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
    I don't see a "Delete" function in the script you posted. It also appears that you aren't storing references to the button instances so that you can destroy them easily... There isn't really any reason to set the DirectoryInfo array to null because GetDirectories returns a fresh array each time you call it.

    P.S. Why are your "dir" and" info" variables static?
     
  3. Cyrussphere

    Cyrussphere

    Joined:
    Jul 1, 2016
    Posts:
    129
    I did indeed have a brainfart moment and I guess I needed to sleep on it. I realize that I completely skipped over deleting all the previous instantiated buttons on this list when it goes inactive so they are still there when the list activates again.

    The actual Delete Game function is on a main controller script. Once the user clicks on one of the saved game buttons it brings them to a new screen with load and delete functions. This is just a small script just to handle the populating of the list on this simple panel. As far as the static thing, that was just to test something else that is completely separate but will be going back to private variables