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

why script work on PC but On android no ......load text php

Discussion in 'Scripting' started by gagac42, Aug 7, 2020.

  1. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    hello why my scrip not load text on android pls help me here is script :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class StavZapasov : MonoBehaviour
    7. {
    8.     public string readText;
    9.  
    10.     public string getURL = "http://myweb/LoadText.php";
    11.  
    12.     public string[] zapasy;
    13.  
    14.     public Text[] StavZapasovText;
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.    private void Start()
    22.     {
    23.         StartCoroutine(GetTheText());
    24.  
    25.      
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     IEnumerator GetTheText()
    30.     {
    31.         string URL = getURL;
    32.         WWW www = new WWW(URL);
    33.         yield return www;
    34.         readText = www.text;
    35.         for (int a = 0; a < 2; a++)
    36.         {
    37.             zapasy = readText.Split(System.Environment.NewLine[a]);
    38.         }
    39.  
    40.         for (int i = 0; i < zapasy.Length; i++)
    41.         {
    42.             StavZapasovText[i].text = zapasy[i];
    43.         }
    44.       }
    45.   }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    How to report problems productively in the Unity3D forums:

    http://plbm.com/?p=220

    Help us to help you.
     
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    Code (CSharp):
    1. <?php
    2.   header("Access-Control-Allow-Origin: *");
    3. ?>
    That line should be at the top of your PHP file or you may run into CORS-related exceptions / nothing will load. Do make sure that's what you want (allowing access to it from anywhere) and that you understand the implications etc.
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Make sure the url is https (secure)
    It is not allowed on Android to download cleartext via http (unsecure)


    also WWW is deprecated by a long time.
    UnityWebRequest is the replacement of WWW. Under the hood of WWW it already has been converted to UnityWebRequest.
     
    PraetorBlue likes this.
  5. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    not works :( but thanks your reply
     
  6. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    on android work the same script but not have split line here is script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ReadText : MonoBehaviour
    7. {
    8.     public Text readText;
    9.  
    10.    public string getURL = "http://myweb/LoadText.php";
    11.  
    12.  
    13.  
    14.  
    15.  
    16.  
    17.  
    18.  
    19.  
    20.  
    21.     void Start()
    22.     {
    23.         StartCoroutine(GetTheText());
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     IEnumerator GetTheText()
    28.     {
    29.         string URL = getURL;
    30.         WWW www = new WWW(URL);
    31.         yield return www;
    32.         readText.text = www.text;
    33.     }
    34.    
    35. }
    36.  
     
  7. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,058
    Still the url is “http://myweb/LoadText.php”
    It should be using https:// due to Android not allowing cleartext over http.
    http:// is unsecure and had been disabled by default since Android... 6 or 7 somewhere?

    for https traffic you require a certificate. The how to’s of setting that up is not my expertise.

    I think unity has/had an option somewhere in the build settings of android to enable insecure traffic over http
    But I’m not on my pc right now so can’t check.
     
  8. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    hello i have android 10 and http: //myweb/LoadText.php“ works cleartext loaded but lines not i think that error is here

    readText Work it on mobile !!!!

    Code (CSharp):
    1. for (int a = 0; a < 2; a++)
    2.         {
    3.             zapasy = readText.Split(System.Environment.NewLine[a]);
    4.         }
    5.         for (int i = 0; i < zapasy.Length; i++)
    6.         {
    7.             StavZapasovText[i].text = zapasy[i];
    8.         }
    unity write error if a > 2

    IndexOutOfRangeException: Index was outside the bounds of the array.
    StavZapasov+<GetTheText>d__5.MoveNext () (at Assets/Uvod/StavZapasov.cs:40)
     
    Last edited: Aug 8, 2020
  9. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,497
    I use UnityWebRequest for insecure traffic over HTTP for WebGL, Windows 7 and Android 6 and have had no issues except for consideration of CORS policy / what I've mentioned earlier in the thread. A similar .htaccess is required with Addressables for remote catalogs/bundles, which I'm including in this thread for anyone else wondering why something is working fine with WAMP but not a live server (do understand what it does before you just copy/paste it though). This goes in the same folder as the Addressables catalog you want to load with LoadContentCatalogAsync, in a file called ".htaccess":
    Code (CSharp):
    1. <IfModule mod_headers.c>
    2.   Header set Access-Control-Allow-Origin "*"
    3. </IfModule>
    Note for WebGL, the usual browser restriction applies of pages loaded via HTTPS needing to load subsequent content over HTTPS as well. The browser can't show people the little lock icon unless all content is being loaded securely.
     
  10. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    but i have a problem IndexOutOfRangeException: Index was outside the bounds of the array.

    my script load text but not set text on Text.text ...readText load data text
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236
     
  12. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    thank you but this didn't help me
     
  13. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    pls help me whosoever :( is it last script from my application


    IndexOutOfRangeException: Index was outside the bounds of the array.
    StavZapasov+<GetTheText>d__4.MoveNext () (at Assets/Uvod/StavZapasov.cs:34)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    What output did you see when you compared the indexing value (i) with the length (or count) of the collection?

    NOBODY here can answer that. Only you can answer that.
     
  15. gagac42

    gagac42

    Joined:
    Apr 24, 2018
    Posts:
    119
    sorry not know what your think ..... i count is 27 ....sorry me english its bad :(