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

Bug Script not working on Android

Discussion in 'Android' started by stoupas, Jul 26, 2023.

  1. stoupas

    stoupas

    Joined:
    Aug 26, 2021
    Posts:
    15
    So I want to upload a game to the google play store but when I test it on my samsung galaxy s10 The script does not work. So the game is a find the word game witch it picks a word randomly and tells you how many letters the first and the last letter of the word and 3 extra. But the script is not working. Here is the script. Hope somebody fixes it.
     

    Attached Files:

    • game.cs
      File size:
      2.7 KB
      Views:
      89
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I assume it works in the editor and your actual question is thus how to debug on your phone.
    Unity has an article about that. You can use ADB Logcat to read the log of your phone, which allows to debug.

    Then it's just the usual. Check if it runs. Check if it executes when it should.
    Check if the results of the execution are what you expect. And so on.

    Edit: I would assume your issue could have to do with the file path. Check how that's done for Android, im not sure if it works the same there as it would on PC. Not an Android dev.
     
    stoupas likes this.
  3. Alan-Liu

    Alan-Liu

    Joined:
    Jan 23, 2014
    Posts:
    354
    On Android, you should use UnityWebRequest to load a file in StreamingAssets, as mentioned in the documentation:
     
  4. manugil

    manugil

    Unity Technologies

    Joined:
    Jan 13, 2020
    Posts:
    41
    Hi @stoupas!

    As mentioned by @Alan-Liu, the problem you are facing is most likely related to the file within the StreamingAssets folder not being properly read when running on an Android device. According to the Application.StreamingAssets documentation: You cannot use synchronous filesystem APIs, such as the C#
    System.IO.File
    class, to access the StreamingAssets folder on the Android platform since it uses a compressed .apk file. These platform returns a URL. Use the
    UnityWebRequest
    class to access the assets.

    Your
    AddRandomWords()
    method should look something like this:
    Code (CSharp):
    1. public void AddRandomWords()
    2. {
    3.     const string fileName = "Words.txt";
    4.     var readFromFilePath = Application.streamingAssetsPath
    5.                              + "/Recall_Chat/" + fileName;
    6.  
    7.     var pWords = new List <string> ();
    8.     if (Application.platform == RuntimePlatform.Android)
    9.     {
    10.         // Need to extract file from Android apk first
    11.         UnityWebRequest www = UnityWebRequest.Get(readFromFilePath);
    12.         var asyncOperation = www.SendWebRequest();
    13.         while (!asyncOperation.isDone) {}
    14.  
    15.         if (www.result == UnityWebRequest.Result.Success)
    16.         {
    17.             pWords = www.downloadHandler.text.Split('\n').ToList();
    18.         }
    19.         else
    20.         {
    21.             Debug.LogError("Error reading the file: " + www.error);
    22.         }
    23.     }
    24.     else
    25.     {
    26.         pWords = File.ReadAllLines(readFromFilePath).ToList();
    27.     }
    28.  
    29.     Word = pWords[Random.Range(0, pWords.Count)];
    30.     wordsToFind.AddRange(pWords);
    31. }
    32.  
     
    Last edited: Jul 26, 2023
  5. stoupas

    stoupas

    Joined:
    Aug 26, 2021
    Posts:
    15
    Yea that works but it doesnt get the last letter and the submit button does not work. It just puts you to the youlost page
     
    Last edited: Jul 26, 2023