This code should be called on login, perform the login and if successfull it should bring the user settings and convert them from json to classes using Newtonsoft Json .net. In the editor and android no problems arise. However when running on IOS the login works alright but when getting the scenes it seems not to be parsing the data correctly.
xcode error:
d__12:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
1 d__12:MoveNext() UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
NullReferenceException: Object reference not set to an instance of an object. at System.Linq.Expressions.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType) [0x00000] in <00000000000000000000000000000000>:0 at
the code in question
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public class DatabaseConnection : MonoBehaviour
{
public MainMenu menu;
public string musername;
public string mpassword;
public string returnArray;
public int currentSession;
public int lastSessUnlcoked=1;
public UserProf userInfo;
public List <SessionSettings> settingsList = new List<SessionSettings> ();
public static DatabaseConnection Instance;
void Awake (){
if(Instance!=null){
Destroy(this);
}else{
Instance=this;
}
}
void Start (){
DontDestroyOnLoad(gameObject);
}
public void Login(){
musername=LoginManager.username;
mpassword=LoginManager.pass;
StartCoroutine(UploadLogin(musername,mpassword));
}
IEnumerator GetSessionData()
{
WWWForm form = new WWWForm();
form.AddField("user_id",userInfo.id);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/spark/clases/UnityGetSettings.php", form))
{
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
Debug.Log (www.downloadHandler.text);
returnArray=www.downloadHandler.text;
var sts = JObject.Parse(returnArray);
var AllSsessions = sts["Items"];
foreach(var currentSession in AllSsessions){
var initials = currentSession["initialAssessment"];
List<Question> InitialAssessment = new List<Question> ();
List<Question> FinalAssessment = new List<Question> ();
List<LevelSettings> levelsList = new List<LevelSettings> ();
List<idStringClass> Summary = new List<idStringClass> ();
List<idStringClass> Debrief = new List<idStringClass> ();
foreach(var initial in initials){
string quest = initial["question"].ToObject<string>();
Debug.Log(initial["Id"]);
var qid = initial["Id"].Value<int>();
string type = initial["typeOfQuestion"].ToObject<string>();
List<Answer>answers=new List<Answer>();
var answrs = initial["answers"];
foreach(var answr in answrs){
var answer = answr.ToObject<Answer>();
answers.Add(answer);
}
var question = new Question(qid, quest, answers, type);
InitialAssessment.Add(question);
}
var finals = currentSession["finalAssessment"];
foreach(var final in finals){
int qid = final["Id"].ToObject<int>();
string quest = final["question"].ToObject<string>();
string type = final["typeOfQuestion"].ToObject<string>();
List<Answer>answers=new List<Answer>();
var answrs = final["answers"];
foreach(var answr in answrs){
int id = answr["Id"].ToObject<int>();
string text = answr["Text"].ToObject<string>();
string feedback = answr["Feedback"].ToObject<string>();
int imgId = answr["imgId"].ToObject<int>();
var answer = new Answer(id, text, feedback, imgId);
answers.Add(answer);
}
var question = new Question(qid,quest, answers, type);
FinalAssessment.Add(question);
}
var levels = currentSession["levels"];
foreach(var level in levels){
string type = level["gameName"].ToObject<string>();
if(type == "gonogo"){
var lvl = level.ToObject<GonogoSettings>();
levelsList.Add(lvl);
}else if (type == "treasure_hunt"){
var lvl = level.ToObject<THuntSettings>();
levelsList.Add(lvl);
}
}
var summaries = currentSession["assessment_summary"];
foreach(var summary in summaries){
var sum = summary.ToObject<idStringClass>();
Summary.Add(sum);
}
var debriefs = currentSession["assessment_debrief"];
foreach(var debrief in debriefs){
var deb = debrief.ToObject<idStringClass>();
Debrief.Add(deb);
}
settingsList.Add(new SessionSettings(1, 1, InitialAssessment, FinalAssessment, levelsList, Summary, Debrief));
}
var json = JsonConvert.SerializeObject(settingsList[0]);
Debug.Log("json"+json);
SceneManager.LoadScene("LevelSelection");
}
}
}
IEnumerator UploadLogin(string username, string password)
{
WWWForm form = new WWWForm();
form.AddField("username",username);
form.AddField("password",password);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/spark/clases/UnityLogin.php", form))
{
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.error);
}
else
{
Debug.Log (www.downloadHandler.text);
returnArray=www.downloadHandler.text;
userInfo=JsonUtility.FromJson<UserProf>(returnArray);
StartCoroutine(GetSessionData());
}
}
}
}