How can I make a C# sharp script execute in a certain order?
I have two scripts that checks if there is network connection and the version of the game (if it's up to date) and then load the game but it's not working properly.
How do I make it follow this pattern?:
Check if there is internet connection
Check the version of the game
Only load the game if there is an internet connection and the version of the game is up to date
CheckNetwork.cs
public Text AlertText;
public GameObject alert;
public string URL = "";
public string CurrentVersion;
string latestVersion;
public GameObject newVersionAvailable;
private void Start()
{
StartCoroutine(LoadTxtData(URL));
}
void Awake()
{
if (Application.internetReachability == NetworkReachability.NotReachable)
{
alert.SetActive(true);
AlertText.text = "Sorry, you have to enable your conection to play this game";
if (Input.GetKey(KeyCode.Escape))
Application.Quit();
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
else
{
alert.SetActive(false);
}
}
public void CheckVersion()
{
if(CurrentVersion != latestVersion)
{
newVersionAvailable.SetActive(true);
}
else
{
newVersionAvailable.SetActive(false);
}
}
private IEnumerator LoadTxtData(string url)
{
UnityWebRequest www = UnityWebRequest.Get(url);
yield return www.SendWebRequest();
latestVersion = www.ToString();
CheckVersion();
}
public void OpenURL(string url)
{
Application.OpenURL(url);
}
Loading.cs
private Image progressBar;
private void Start()
{
StartCoroutine(LoadAsyncOperation());
}
public IEnumerator LoadAsyncOperation()
{
AsyncOperation gameLevel = SceneManager.LoadSceneAsync(1);
while (gameLevel.progress < 1)
{
progressBar.fillAmount = gameLevel.progress;
yield return new WaitForEndOfFrame();
}
}
Answer by xxmariofer · Mar 23, 2019 at 10:18 PM
try changing removing the start method from the loading.cs
private Image progressBar;
public IEnumerator LoadAsyncOperation()
{
AsyncOperation gameLevel = SceneManager.LoadSceneAsync(1);
while (gameLevel.progress < 1)
{
progressBar.fillAmount = gameLevel.progress;
yield return new WaitForEndOfFrame();
}
}
and in the checknetwork script change the checkversion to this
public void CheckVersion()
{
if(CurrentVersion != latestVersion)
{
newVersionAvailable.SetActive(true);
}
else
{
newVersionAvailable.SetActive(false);
loading.StartCoroutine(LoadAsyncOperation());
}
}
you need to create a
public Loading loading;
var and assign it in the inspector
Your answer
Follow this Question
Related Questions
Having multiple UNET errors that I believe are caused by unity itself. Am I doing something wrong? 1 Answer
Why does NetworkServer.ReplacePlayerForConnection not recognize the instantiated object parameter? 0 Answers
How to solve "Array Index is out of range" 1 Answer
ClientRpc not functioning 0 Answers