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.