-3

i tried this in a c# console aplication and it worked but as soon as i shoved it in a unity program it diddent work plz help

using System.Collections.Generic;
using UnityEngine;
using RestSharp;

public class corona : MonoBehaviour
{

    RestSharp.RestClient client = new RestClient("https://www.worldometers.info/coronavirus/");
    RestSharp.RestRequest request = new RestRequest(Method.GET);

    string content = client.Execute(request).Content;
    string[] words = content.Split(' ');
    string line = words[832];
    string[] lineCut = line.Split('>');
    string Scases = LineLineCut[0] + LineLineCut[1];'''
derHugo
  • 83,094
  • 9
  • 75
  • 115
Bjørn Pollen
  • 43
  • 1
  • 6

1 Answers1

1

The code you've shown would not have worked in a C# console application either, because you have field initializers referring to other fields (e.g. the content field initializer using request).

You probably want to put all of that code in a method instead:

public class Corona : MonoBehavior
{
    public void SomeMethod()
    {
        RestSharp.RestClient client = new RestClient("https://www.worldometers.info/coronavirus/");
        RestSharp.RestRequest request = new RestRequest(Method.GET);

        string content = client.Execute(request).Content;
        string[] words = content.Split(' ');
        string line = words[832];
        string[] lineCut = line.Split('>');
        // Note: you haven't shown a declaration for LineLineCut.
        // You may have just meant lineCt
        string Scases = LineLineCut[0] + LineLineCut[1];
        // ...
    }
}

I strongly suspect that your working console application had code similar to this.

You may need to change the RestSharp code as well to a more Unity-centric HTTP stack - I don't know whether RestSharp is available in Unity. I'd also recommend trying to find a data source which isn't as brittle - accessing words[832] feels like it's just waiting to go wrong.

You'll then need to work out how to call the method at the right time, in some Unity-specific way, as well. But the first thing to get past is your field initializer compiler error.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194