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

String to Float conversion not working :(

Discussion in 'Scripting' started by heinickesg, Jun 11, 2014.

  1. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DatabasePosition : MonoBehaviour
    5. {
    6.     private string secretKey = "kep"; // Edit this value and make sure it's the same as the one stored on the server
    7.     public string addPosURL = "url"; //be sure to add a ? to your url
    8.     public string getPosXURL = "url";
    9.     public string getPosYURL = "url";
    10.     public string getPosZURL = "url";
    11.     public MD5Test MD5Test;
    12.  
    13.     public string returnedvalueX;
    14.     public string returnedvalueY;
    15.     public string returnedvalueZ;
    16.  
    17.     public float xToFloat;
    18.     public float yToFloat;
    19.     public float zToFloat;
    20.  
    21.     public bool valueReturned = false;
    22.  
    23.     public GUIText test;
    24.  
    25.  
    26.  
    27.    
    28.  
    29.     public IEnumerator GetPlayerPosition(string user)
    30.     {
    31.         test.guiText.text = "Loading Position";
    32.  
    33.         string getposX_url = getPosXURL + "user=" + user;
    34.         Debug.Log ("X Url" + getposX_url);
    35.  
    36.         string getposY_url = getPosYURL + "user=" + user;
    37.         Debug.Log ("Y Url" + getposY_url);
    38.  
    39.         string getposZ_url = getPosZURL + "user=" + user;
    40.         Debug.Log ("Z Url" + getposZ_url);
    41.  
    42.         WWW X_get = new WWW(getposX_url);
    43.         yield return X_get;
    44.  
    45.         WWW Y_get = new WWW(getposY_url);
    46.         yield return Y_get;
    47.  
    48.         WWW Z_get = new WWW(getposZ_url);
    49.         yield return Z_get;
    50.      
    51.         if (X_get.error != null)
    52.         {
    53.             print("There was an error getting the high score: " + X_get.error);
    54.         }
    55.         else
    56.         {
    57.  
    58.  
    59.             returnedvalueX = X_get.text;
    60.             returnedvalueY = Y_get.text;
    61.             returnedvalueZ = Z_get.text;
    62.  
    63.             xToFloat = float.TryParse(returnedvalueX);
    64.             yToFloat = float.TryParse(returnedvalueY);
    65.             zToFloat = float.TryParse(returnedvalueZ);
    66.  
    67.  
    68.  
    69.             valueReturned = true;
    70.         }
    71.     }
    72.  
    73. }
    Can someone help me out with this script? I have been trying to find answer for quite some time to no avail.

    So a rundown of how this script is suppose to work.

    It sends a username to my php script asking for the X Y and Z of the player. Long long story short it returns me a number which gets put into returnedvalue (XYZ respectively)

    From this point I need to convert the web result (the returnedvalue variables), which is a string for some reason to a float to use as XYZ coordinates.

    Can someone help me get this working?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    TryParse returns a bool indicating success and takes an out parameter to which the value is applied if successful. So you'd want something like this

    Code (csharp):
    1.  
    2. valueReturned = float.TryParse(returnedvalueX, out xToFloat) && float.TryParse(returnedvalueY, out yToFloat) && float.TryParse(returnedvalueZ, out zToFloat);
    3.  
     
  3. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    What error are you getting? I expected float.TryParse(float, out float) to need two parameters, not just one.
    Code (csharp):
    1. // Set xToFloat. If this is false, then we failed.
    2. if (!float.TryParse(returnedvalueX, out xToFloat)) {
    3.   Debug.Log("Unable to parse the string " + returnedvalueX + " to a float.");
    4. }
     
  4. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Im getting a character unrecognized error and I found out why.... I tested it by hard coding it and it worked fine.

    The problem is when the browser outputs... When I view it in the browser i get just number, However when it gets sent to unity it looks like this:

    31
    <!-- Hosting24 Analytics Code -->
    <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
    <!-- End Of Analytics Code -->

    I only want the "31" whats the rest of this stuff?!

    EDIT:

    I have confirmed it is client side, I accessed the script in my browser and hit ctrl+a then copied and pasted into notepad, it only pasted the number, Why is unity reading the rest of that stuff? am I outputting incorrectly in my php script?

    EDIT:

    Is there a way to check for that analytic stuff and simply delete it leaving just the number?
     
    Last edited: Jun 12, 2014
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    When you copy and paste, are you just pasting what you see in a browser? That other part is a script by your hosting service to keep track of page counts and it usually doesn't get displayed in a browser. I don't know if your hosting service does that automatically or if it's a setting on your hosting account.

    EDIT: If you look at "Page Source" when you access the page in a web browser, is that script appearing there too? If so, then it's definitely server-side.
     
  6. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    I was using the select all command. All I see on the page is the number. I have no doubt it is server side because im really new to php. Here is the script for getting the X value, am I calling stuff wrong? For some reason that While loop at the end seems unnecessary and might be causing the problem. Any thoughts?

    PHP:
    <?php
        
    // Send variables for the MySQL database class.
        
    $database mysql_connect('host''user''pass') or die('Could not connect: ' mysql_error());
        
    mysql_select_db('database') or die('Could not select database');

        
    $user mysql_real_escape_string($_GET['user'], $database); 

    $query  "SELECT user, PlayerX  FROM Users WHERE user = '$user'";
    $result mysql_query($query) or die (mysql_error());

    while(
    $row mysql_fetch_array($result))
    {
        echo 
    "{$row['PlayerX']}";




             
    ?> 
     
  7. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Oh man, it's been a while since I dealt with php and SQL code. I'm not going to be able to help there!

    I really think that 3 lines dealing with analytics is being added by your server and has nothing to do with your php/SQL script. I would guess that those 3 lines are being added to every single web page served on that web host.

    If you do want to see the page source, I don't know what web browser you're using, but in Firefox I can just right click to bring up a menu and select "View Page Source".
     
  8. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Let me take a look, Im using 000webhost. Don't suppose you know how I would disable this?
     
  9. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    You are correct, I viewed the source and I see this:

    841.928
    <!-- Hosting24 Analytics Code -->
    <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
    <!-- End Of Analytics Code -->
     
  10. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
  11. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48
    Ok I went into control panel and yes they were adding code to my stuff. Thanks so much! Would not have thought of that!
     
  12. heinickesg

    heinickesg

    Joined:
    Jan 25, 2013
    Posts:
    48

    If I can disable it entirely will that be ok? or should i just try to remove it?

    EDIT:

    Disabled it entirely and the code works flawlessly, Thanks all for your suggestions!