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

Need help with scripting problems - API

Discussion in 'Scripting' started by JezzaJeremy, May 8, 2021.

  1. JezzaJeremy

    JezzaJeremy

    Joined:
    Apr 28, 2021
    Posts:
    5
    Hey, I'm a noob, currently doing an assignment.

    I don't understand most terms.

    I'm trying to create a simple weather API function
    There seems to be no issues with the code, but I get errors, and the api doesn't seem to show the URL when adding it as a component in the EventSystem section.

    upload_2021-5-8_14-47-22.png

    The errors are:

    Assets\Scripts\WeatherApi.cs(57,38): error CS1061: 'string' does not contain a definition for 'Parse' and no accessible extension method 'Parse' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    and

    Assets\Scripts\WeatherApi.cs(39,31): error CS1061: 'string' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    I'm quite lost.

    I've tried reinstalling Unity. Something is missing but I'm no veteran. The script, so far, in the API is below:


    -----------------------------------------------------------------------

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using SimpleJSON;
    5. using UnityEngine.UI;
    6.  
    7. public class WeatherAPI : MonoBehaviour
    8. {
    9.     // store API location into a string
    10.     public string url = "https://fcc-weather-api.glitch.me/api/current?lat=35&lon=139";
    11.     public string currentLocation;
    12.     public float currentTemp;
    13.     public float currentLowTemp;
    14.     public float currentMaxTemp;
    15.  
    16.     // Display Inputs
    17.     public Text displayLocation;
    18.     public Text displayTemp;
    19.     public Text displayLowTemp;
    20.     public Text displayHighTemp;
    21.  
    22.  
    23.  
    24.     // set up URL request to locate url
    25.     // access JSON code
    26.     // initialisation of API
    27.     IEnumerator Start()
    28.     {
    29.         // set up www request
    30.         WWW request = new WWW(url);
    31.         yield return request;
    32.  
    33.         if (request.error == null || request.error == "")
    34.         {
    35.             // successful call
    36.             // parse the JSON code
    37.             Debug.Log("Response:" + request.text);
    38.             string weather = JsonUtility.ToJson(request.text);
    39.             Debug.Log(weather.text);
    40.             SetWeather(request.text);
    41.  
    42.  
    43.         }
    44.         else
    45.         {
    46.             // if the call to API has failed
    47.             Debug.Log("Error: " + request.error);
    48.         }
    49.  
    50.  
    51.  
    52.     }
    53.  
    54.     // Set Weather Variables Function
    55.     void SetWeather(string jsonString)
    56.     {
    57.         var weatherJson = jsonString.Parse(jsonString);
    58.         // issuing variables the data they need
    59.         currentLocation = weatherJson["name"].Value;
    60.         currentTemp = weatherJson["main"]["temp"].AsFloat;
    61.         currentLowTemp = weatherJson["main"]["temp_min"].AsFloat;
    62.         currentMaxTemp = weatherJson["main"]["temp_max"].AsFloat;
    63.         Debug.Log(currentLocation);
    64.         Debug.Log(currentTemp);
    65.         Debug.Log(currentMaxTemp);
    66.         Debug.Log(currentLowTemp);
    67.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    The important parts of an error message are:
    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
    Bunny83 and JezzaJeremy like this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,531
    This line makes no sense at all:
    var weatherJson = jsonString.Parse(jsonString);


    Maybe you did a plaintext search and replace, somehow? Because the line should read

    var weatherJson = JSON.Parse(jsonString);
     
    JezzaJeremy likes this.
  4. JezzaJeremy

    JezzaJeremy

    Joined:
    Apr 28, 2021
    Posts:
    5
    thank you, this clears up one of the errors. For some reason debug and public text doesn't light up still
     
  5. JezzaJeremy

    JezzaJeremy

    Joined:
    Apr 28, 2021
    Posts:
    5
    the error is
    Assets\Scripts\WeatherAPI.cs(39,31): error CS1061: 'string' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Delete the
    .text
    from both line:
    Code (CSharp):
    1. Debug.Log(weather.text);
    2. SetWeather(request.text);
    Also why are you sending the request to the SetWeather?

    All on these notes, my advise is that you need to go back to the basics, you don't know the basic syntax.
    Here is a good one:
     
  7. JezzaJeremy

    JezzaJeremy

    Joined:
    Apr 28, 2021
    Posts:
    5
    Thanks, I don't know the basics. I'm copying word from word a tutorial from a teacher.

    After doing that I get:

    Assets\Scripts\WeatherAPI.cs(40,24): error CS1503: Argument 1: cannot convert from 'UnityEngine.WWW' to 'string'
     
  8. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,913
    Well, it's far from word for word. Check your tutorial again. And watch what I linked.
     
  9. JezzaJeremy

    JezzaJeremy

    Joined:
    Apr 28, 2021
    Posts:
    5
    Thanks, I was having a dyslexic moment. All good now.