- Home /
Unity keep crashing when loading scene from a couritne (Unity 5.x)
I am trying to send a POST request to an API and load a scene when the![alt text][1]script gets a response, but it's crashing the Unity.
Version 5.x
Thanks in advance.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
public class LogInGamer : MonoBehaviour
{
[SerializeField] private CredentialsConfig credentialsConfig;
[SerializeField] private string PORT;
[SerializeField] private string ServerLink;
[SerializeField] private string sucsessfulLoginScene;
[SerializeField] private InputField gamerIdInputField;
[SerializeField] private InputField gamerPasswordInputField;
private string gamerId = "";
private string password = "";
private IEnumerator coroutine;
private bool _isLoggedIn;
[SerializeField] private GameObject registrationPanel;
private void Start() {
PORT = credentialsConfig.PORT;
ServerLink = credentialsConfig.URL;
}
public void ActivateRegistationPanel()
{
registrationPanel.SetActive(true);
}
public void TakeIdInput()
{
gamerId = gamerIdInputField.text;
}
public void TakePasswordInput()
{
password = gamerPasswordInputField.text;
}
public void HitLogInApi()
{
coroutine = LogIn();
StartCoroutine(coroutine);
}
IEnumerator LogIn()
{
WWWForm form = new WWWForm();
form.AddField("gamerid", gamerId);
form.AddField("password", password);
form.AddField("projectId", credentialsConfig.projectId);
using (UnityWebRequest www = UnityWebRequest.Post(ServerLink + ":" + PORT + "/unity/login", form))
{
www.SetRequestHeader("Accept", "application/json");
#if !UNITY_5
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
if(www.responseCode == 200)
{
this.gameObject.SetActive(false);
}
var c = www.downloadHandler.text;
Debug.Log("Response-> " + c);
}
#else
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
if (www.responseCode == 200)
{
//SceneManager.LoadSceneAsync(sucsessfulLoginScene, LoadSceneMode.Single);
SceneManager.LoadScene(sucsessfulLoginScene, LoadSceneMode.Single);
}
var c = www.downloadHandler.text;
Debug.Log("Response-> " + c);
}
#endif
}
}
}
First, don't post code in screenshots. Use the code button (101010) ins$$anonymous$$d.
Second, look in your log file and let us know what that says about the crash.
Yes, I have checked it and this is only the one script Unity Project has. After removing these lines works fine but I want to change the scene if login is successful.
//Scene$$anonymous$$anager.LoadSceneAsync(sucsessfulLoginScene, LoadScene$$anonymous$$ode.Single);
Scene$$anonymous$$anager.LoadScene(sucsessfulLoginScene, LoadScene$$anonymous$$ode.Single);
Thanks
You really need to look at the log file https://docs.unity3d.com/$$anonymous$$anual/LogFiles.html
Answer by yolkfolk · Nov 26, 2019 at 05:59 AM
You're not instantiating sucsessfulLoginScene
anywhere.
In Unity, go to File > Build Settings, and set your sucsessfulLoginScene
to one of the values in "Scenes In Build". For example, sucsessfulLoginScene = "Scenes/LoginScene"
.