Why is USB Storage being detected only in some android devices and not all?

I am working on a sample project in unity android where application creates a text in an OTG pen drive connected to the android phone, the problem is my code works fine in some devices but it doesn’t work in some devices. I am unable to figure out the main cause even through the logcat extreme as it doesn’t throw any error at all that specifies code error.

I tried adding text logs to print text in between the code to see which texts are not printed, which would further tell me that after which line code stopped working but all the texts are printed.

This is my code that detects the USB and returns true if a text file is present in USB:

public bool LocateFile()
{

    textfileData = null;

  using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
   {
    using (AndroidJavaObject context = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
     {
     // Get all available external file directories (emulated and USBdrives)
         AndroidJavaObject[] externalFilesDirectories =  context.Call<AndroidJavaObject[]>("getExternalFilesDirs", (object)null);

                 AndroidJavaObject emulated = null;
                 AndroidJavaObject USBdrive = null;


   for (int i = 0; i < externalFilesDirectories.Length; i++)
                      {
                         AndroidJavaObject directory = externalFilesDirectories*;*

using (AndroidJavaClass environment = new AndroidJavaClass(“android.os.Environment”))
{
bool isRemovable = environment.CallStatic(“isExternalStorageRemovable”, directory);
bool isEmulated = environment.CallStatic(“isExternalStorageEmulated”, directory);
if (isEmulated)
{
emulated = directory;
}
else if (isRemovable && isEmulated == false)
{
USBdrive = directory;
}
}
}

// Return the USBdrive if available
if (USBdrive != null)
{
if (File.Exists(USBdrive.Call(“getAbsolutePath”) + “/serial.txt”))
{
textfileData = File.ReadAllText(USBdrive.Call(“getAbsolutePath”) + “/serial.txt”);
return true;
}

}
}
}
}
Update: I edited my code to print a text of length of GetExternalFilesDir and it only reads 1 i.e. internal storage. How do I get this to detect external storage as well?

If there is a problem with my question at least let me know so I can do something about it. How can you leave a developer ignored like that?

did you solve the problem?
with your code my usb is detected but i can not read the file (but it get if exist or not)