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
  4. Dismiss Notice

String Array using System.IO GetFiles

Discussion in 'Scripting' started by addscion, Jul 23, 2021.

  1. addscion

    addscion

    Joined:
    Jan 12, 2018
    Posts:
    13
    This should return the names of files (including their paths) in the specified directory and populate an array.
    Code (CSharp):
    1. public string[] Tokens = System.IO.Directory.GetFiles("Assets/Tokens");
    It is not working, and I have no errors in the console. The folder has .txt files.


    While the game was in Play mode, I removed public from the array and saved, then readded public to the array, saved again, and the array updated in the inspector.

    How do I get the inspector to update if they are in fact populating the array?
     
    Last edited: Jul 23, 2021
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Can you not just get the Files inside of the Start() method?
     
  3. addscion

    addscion

    Joined:
    Jan 12, 2018
    Posts:
    13
    I do, the public array does not show this in the inspector
    When I make this private, it happens.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,779
    If you have a list of these Tokens and want to prepopulate a string array with them, just make a little editor inspector button to do it.

    If you want these read fresh from that file every time you run, do it in Start() and do NOT mark it public. Marking it public makes Unity ALSO serialize the contents of it.

    If you want a custom view on them in your object, make a custom read-only editor inspector window.