Search Unity

Cannot implicitly convert type 'char' to 'string'

Discussion in 'Scripting' started by girlinorbit, Sep 10, 2019.

  1. girlinorbit

    girlinorbit

    Joined:
    Apr 27, 2019
    Posts:
    115
    Hey, so I have a script that returns data from a PHP file, and splits the return data into an array. I have used the same method many times and it has worked fine. I don't know what I'm doing wrong, but I keep getting these errors:

    "Assets\_Scripts\LoginUser.cs(429,68): error CS1503: Argument 1: cannot convert from 'string' to 'char'"
    AND
    "Assets\_Scripts\LoginUser.cs(442,51): error CS0029: Cannot implicitly convert type 'char' to 'string'"
    for the ones in the foreach loop. Please help!

    Code (CSharp):
    1. string questFile = "https://MYWEBSITE.com/GetUserQuests.php";
    2.  
    3.                     WWWForm questform = new WWWForm();
    4.                     questform.AddField("user_id", id);
    5.  
    6.                     UnityWebRequest questWWW = UnityWebRequest.Post(questFile, questform);
    7.                     yield return questWWW.SendWebRequest();
    8.  
    9.                     if (questWWW.isNetworkError || questWWW.isHttpError)
    10.                     {
    11.                         Debug.Log(questWWW.error);
    12.                         Debug.Log("quest info didn't worked");
    13.                     }
    14.                     else
    15.                     {
    16.                         Debug.Log(questWWW.downloadHandler.text);
    17. /*LINE 428 */
    18.                         string questReturnData = questWWW.downloadHandler.text;
    19.                         string[] questData = questReturnData.Split("--");
    20.  
    21.                         /* 0: mysqli_num_rows
    22.                          * 1: quest_log_id
    23.                          * 2: quest_id
    24.                          * 3: quest_char
    25.                          * 4: quest_item
    26.                          * 5: quest_item_quant
    27.                          * 6: collected
    28.                          */
    29.  
    30.                         foreach (string questReturn in questData) {
    31.  
    32. /*LINE 442 */
    33.                             string quest_log_id = questReturn[1];
    34.                             string quest_id = questReturn[2];
    35.                             string quest_char = questReturn[3];
    36.                             string quest_item = questReturn[4];
    37.                             string quest_item_quant = questReturn[5];
    38.                             string collected = questReturn[6];
    39.  
    40.                             Debug.Log("foreach Ran");
    41.  
    42.                         }
    43.                     }
    I do understand that I do not start at 0 in my array. This is intentional.

    Thanks for any help in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    questReturn is a string.

    questReturn[n] is a char.

    You are both iterating the questData collection with your foreach, PLUS you think you are indexing each string from that with [1], [2], etc., but you are actually pulling chars out of the questReturn string.
     
  3. girlinorbit

    girlinorbit

    Joined:
    Apr 27, 2019
    Posts:
    115
    Okay, but how do I fix it?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    You could just apply .ToString() to the char to make it a string, but I don't think you want that based on what I can infer from your code above, but only YOU know your data.

    I recommend you re-visualize your data and decide how you want to go about chopping it up, because I suspect the above code does NOT cut it up in a meaningful way, unless I am mistaken.
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If each record is separated by a "--" then each questReturn will have all the data you mention in the comments starting on line 21 I'd assume. Not sure how you have that data formatted, but if it is similar to the comment I'd use a regex to grab each individual value you're interested in. Teaching how to use a regex is too big a subject to teach in a forum post, but there's lots of tutorials on it.
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    At a quick glance, it seems to me you don't need the foreach loop as that is selecting characters from strings. It looks to me like you meant to assign the elements from the array which is a result of splitting your string, like so;

    Code (CSharp):
    1.         string quest_log_id     = questData[1];
    2.         string quest_id         = questData[2];
    3.         string quest_char       = questData[3];
    4.         string quest_item       = questData[4];
    5.         string quest_item_quant = questData[5];
    6.         string collected        = questData[6];
     
    EdGunther and Kurt-Dekker like this.