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.

Accessing Text File From Server...

Discussion in 'Multiplayer' started by Dinesh Balu, May 4, 2011.

  1. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Hi...
    I'm creating a demo project , in which the client unity application has to access a text file in the server.
    Can I access a text file like this (or) do I have to use XML file instead of text file...
     
  2. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    What's wrong with this code....

    Code (csharp):
    1.  
    2. var fileurl="http://192.168.1.23/unity/sample.txt";
    3.  
    4. function ReadFile()
    5. {
    6.    var File=WWW(fileurl);
    7.    yield File;
    8.    if(File.error) {
    9.          print("There was an error getting the high score: " + File.error);
    10.      }
    11.      else {
    12.     //var fileContents =
    13. print(  File.text);
    14. /*
    15.     var lines = fileContents.Split("\n"[0]);
    16.     for (line in lines) {
    17.         print (line);
    18.     }*/
    19. }
    20. }
    The file is not in my system , it's in my local network system with local IP 192.168.1.23

    I've tried by changing the url to "http://192.168.1.23/unity/display.php" and it worked..

    I just can't access txt and xml file..

    when I tried to access txt or xml file ...
    it gives me the following error

     
  3. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Somebody.....
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    you do access it since you get the html output
     
  5. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    but I'm accessing a text file.....

    check the error though... it's a html error file generated by Unity

     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    it doesn't matter if the extension of the file is *.txt or *.html or whatever.
     
  7. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I don't know how to explain it ....

    Here it is .....

    I need to access a text file located in a system with IP:192.168.1.23... it's in my local network.
    The Content of the text file is "Hello World !!" already written by me...

    when I access using the above code, it gives me the error code in html format...

    I have accessed a php script with the same code I used before and it worked ....

    I don't understand .. why I'm getting this error report...
     
  8. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    help me... I'm stuck with this...
     
  9. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Code (csharp):
    1. var url = "http://localhost/sample.txt";
    2.  
    3. function Start () {
    4.     var w = new WWW(url);
    5.     yield w;
    6.     Debug.Log(w.text);
    7. }
    thats all you need to do to read a file
     
  10. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Thats all I did...

    Check yourself for the following url and tell me what it shows... I'm getting error report with this url too...
    may be a problem with unity 3.3

     
  11. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    i just tried and i'm getting the text without errors
    it's not related to unity
     
  12. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    :'(

    Why am I getting this error....
     
  13. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    you must have a problem somewhere in one of your scripts, make a new clean project, drop my script on the camera and press play to make sure it's working
     
  14. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Yeah I should try that...
     
  15. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    That is a 404 Error message.
    That is something you get when you are trying to access a webpage that doesn't exist and your web server is not setup to display another page. This seems to me like an Apache setup issue, not a Unity issue.

    I am not too clued up on the Apache stuff, but from what it looks like to me, is that you are sending the request to your Apache Web server through port 80 or 8080 or something and apache is trying to locate the file you are after but not finding it. By default, if this were a web browser, that page would have appeared so since it isn't, it just returns the code that it would have displayed.

    Now, with my VERY limited knowledge of apache etc, I seem to recall something about a file that needs to be configired, something like /bin/etc/httaccess.ini or something like that. I would venture a guess that your Apache is not setup to recognize TXT or XML files as valid extensions???? Doubt that is it, though. The alternative could be that your server is setup that anything that comes in to [your IP]:80/ goes to ~/Web/ or c:\mysite\ or whatever and thus you need to place your data in THAT folder or you need to create a subdomain that points to where you DO want to keep the files and use THAT address instead.

    Alternatively, it could be a simple matter of your system being case sensitive and you are searching for myfile.txt while it is in fact Myfile.txt...

    You will have to forgive me, my knowledge of Apache is really very limited but I thought I'd point out the obvious fact that this is a 404 error message that is generated by web servers, not Unity. Perhaps someone who knowledgeable than myself can solve your issue from here...
     
    Last edited: May 4, 2011
  16. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I'm getting this error....
    I didn't include standard packages ... does it have anything to do with this?
     
  17. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255


    Thanks MrDude,
    This explains my error though the problem is not solved...

    The file is exactly in the same location where I placed my PHP scripts, those PHP scripts are accessed successfully. So like you said.. this should be configuration problem.

    Problem is my knowledge in apache stuff is NIL ...

    I hope I will get the problem solved soon... Try to help me as possible as you can...

    Thanks again...
     
  18. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    @Mr.Dude : but appels just used the same code for the same url and he said this

    how come it works for him but not for me....? :(
     
  19. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    it's not an apache problem either, when you browse to the file in a webrowser you can read it fine.
    just make a clean project, drop my script in it with the url you want to read, works fine.
     
  20. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I've tried it in a new scene.... didn't work... let's try that in new project...

    this is not working too...
     
    Last edited: May 4, 2011
  21. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    What's the problem.... :(
     
  22. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    stating that it doesn't work will not help you, at the very least you should post your script and the errors you are getting.
    you could also try and debug your script, debug all the steps to see where it goes wrong.
     
  23. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    The Script code is,
    Code (csharp):
    1. var url = "http://www.textfiles.com/100/adventur.txt";
    2.  
    3. function Start () {
    4.     var w = new WWW(url);
    5.     yield w;
    6.     Debug.Log(w.text);
    7. }

    I've attached this script to Main Camera,

    The Name of the Project and the script is same FileURL and name of the scene is File...

    The error I get is:

     
  24. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I tried to print the url with this code
    Code (csharp):
    1. var url = "http://www.textfiles.com/100/adventur.txt";
    2.  
    3. function Start () {
    4.     var w = new WWW(url);
    5.     print("Loading");
    6.     yield w;
    7.     print(w.url);
    8.     Debug.Log(w.text);
    9. }
    but the output is

    this is not the url I gave in the script....

    then the error
     
  25. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    so it works.
    the variables in the inspector have precedence on the ones in your script.

    this is not an error, it's a debug message
     
  26. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    So, tonight I started working on a login system of my own. Started from scratch, took my first plunge into PHP and managed to get the whole thing working just the way I wanted it to. I am really bloody chuffed with myself. But what was interesting was that at one point I got theexact same problem as Dinesh. That same "<?xml[bla bla bla" message he got.

    Figured out what it was and how to get it working. So, listen up, bru.

    This is what happened to me:
    My Apache is setup to run my website from folder A and I have an old copy of my website in folder B also and then look identical. So I placed my PHP files in folder B and ran my game. Inside Unity the WWW always returned without an error but it always showed me that error you got. So I took a chance and tried to load a file that I know does not exist and guess what? No error. WWW still returned true and gave me the same info. This was when it hit me. My Apache is setup to return the default.html page whenever it cannot find a file it is looking for. So here I was, trying to access a php file in the completely wrong folder, Apache just returned the default.html page every time and Unity was happy... So when I looked at the source code for default.html guess what I found? That message you and I kept getting was the source code for the file returned by Apache.

    So, I went into my xampp folder, went to the htdocs shortcut and copied my php files over to there. Now they are in the root of my localhost and I tried running the game again. Perfect. 0 problems and everything works fine.

    So in my case, that was the issue, I was placing my files in the wrong folder and as a result Apache always returned default.html. Hope this solves your issue, also.
     
  27. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    I change the declaration line with private keyword...
    Code (csharp):
    1. private var url = "http://www.textfiles.com/100/adventur.txt";
    2.  
    3. function Start () {
    4.     var w = new WWW(url);
    5.     print("Loading");
    6.     yield w;
    7.     print(w.url);
    8.     Debug.Log(w.text);
    9. }
    Now it works fine....

    The problem is with particular file...

    and something affecting the url so that it's not changing... when I put private keyword the url is not affected by that something... access was denied...
     
  28. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255

    Yeah,... Thanks Mr.Dude

    My problem is solved with a private keyword...

    Why should I use private keyword?
    After you getting these kind of bugs , if you tried to change the url, It's not changing and if you use private keyword for the url variable, it works perfectly...


    So.. My problem was with the first url I used "hhtp://192.168.1.23/unity/sample.txt" , I hope this file is not there or in another name, I have to check the directory later (admin is not here).



    Thanks bros...

    So Now we did read a text file from url...

    Can we write in a text file in some url?
     
  29. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    making the variable private does not affect the code itself, it only hides the variable in the inspector.
    you could just change the variable in the inspector instead of the script.
     
  30. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    yeah... you r right... something is happening... why does the variable's value is not changing from the script but changed from editing the inspector.
     
  31. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    as stated before
     
  32. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Um... Thanks appels..

    So Can we write on a text file in some url?
     
  33. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    nope, you can post a www form to a php and let the php write the file
     
  34. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    K... That's what I thought.... Thanks appels...
     
  35. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Dinesh, just to elaborate on Apple's statement above, to explain it a bit differently:
    If you set a value for a variable in your script and then attach that script to an object, the first time your object then loads that script, it takes the value you gave it in your script BUT then the object has a value...

    So for example, if you say:
    Code (csharp):
    1. var mystring : String = "hello world"
    ...then when you first give that script to an object like, say, a static character standing at your town entrance, that character now has your component and it has the value "hello world" stored in mystring.

    This is where apples statement comes into play. Wether you set the value in the inspector or set the value in code doesn't matter. All that matters is that it received an initial value. Now that it has a value, that value is set and takes precidence over code written in your script. Meaning, if you now go into your script and change the code to:
    Code (csharp):
    1. var mystring : String = "hack the planet"
    , when you drop that script onto a NEW game object, that object will get the value "hack the planet" but the first object, since it already HAS a value in the inspector already, that value does not change.

    You have two options on how to change that value:
    1. Change it by hand in the inspector
    2. Make the variable private

    What I do sometimes if i have a lot of objects that I need to change the string like this, instead of changing them each one by one, I make the variable private and wait for the value to disappear from the inspector. Then I simply remove the private again and now all the objects have the new value :)
     
  36. Dinesh Balu

    Dinesh Balu

    Joined:
    Mar 10, 2011
    Posts:
    255
    Thanks Mr.Dude.... That's more detailed...
     
  37. MrDude

    MrDude

    Joined:
    Sep 21, 2006
    Posts:
    2,569
    Nuff said
    I think it's time for me to unsubscribe from this nearly 7 year old thread...
     
    TwoTen and Munchy2007 like this.
  38. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
  39. mtvalle

    mtvalle

    Joined:
    Nov 12, 2018
    Posts:
    1
    Unbelievable!
    I had exactly the same problem. But at trying acess an url of a php file with parameters to call a function to return values to Unity. Before trying change the apache configurations, I just changed the url variable to private in the Unity and it worked perfectly.
    Thanks everybody!
     
  40. dabitnk10

    dabitnk10

    Joined:
    Jan 18, 2020
    Posts:
    2
    It works for me in Unity 2021.3.11f1, with RETURN in YIELD and posting in Couroutine, like this:

    public class readCredentials : MonoBehaviour
    {
    private string url = "https://YOUR-URL.com/credentials.txt";

    void Start()
    {
    StartCoroutine(readFile());
    }
    IEnumerator readFile()
    {
    var w = new WWW(url);
    print("Loading");
    yield return w;
    print(w.url);
    Debug.Log(w.text);
    }