I have a problem and when executing unity I get this error
ArgumentNullException: String reference not set to an instance of a String. Parameter name: s System.Text.Encoding.GetBytes (System.String s) (at :0) UnityEngine.WWWForm.AddField (System.String fieldName, System.String value, System.Text.Encoding e) (at C:/buildslave/unity/build/Modules/UnityWebRequest/Public/WebRequestUtils.cs:63) UnityEngine.WWWForm.AddField (System.String fieldName, System.String value) (at C:/buildslave/unity/build/Modules/UnityWebRequest/Public/WebRequestUtils.cs:55) Web+d_4.MoveNext () (at Assets/tt/Web.cs:91) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) d3:MoveNext() (at Assets/tt/item.cs:45) UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) item:b1_0(String) (at Assets/tt/item.cs:17) d_5:MoveNext() (at Assets/tt/Web.cs:125) UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
codigo item
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using SimpleJSON;
using System.IO;
public class item : MonoBehaviour
{
Action<string> _createItemsCallback;
// Start is called before the first frame update
void Start(){
_createItemsCallback = (jsonArrayString)=>{
StartCoroutine(CreateItemsRoutine(jsonArrayString));
};
CreateItems();
}
// Update is called once per frame
public void CreateItems()
{
string iduser = Main.Instance.Userinfo.Iduser;
StartCoroutine(Main.Instance.Web.GetItemsIDs(iduser, _createItemsCallback));
}
IEnumerator CreateItemsRoutine(string jsonArrayString){
JSONArray jsonArray = JSON.Parse(jsonArrayString) as JSONArray;
for (int i = 0; i < jsonArray.Count; i++){
bool isDone = false;
string idlugar = jsonArray[i].AsObject["idlugar"];
JSONObject itemInfoJson = new JSONObject();
Action<string> getItemInfoCallback = (itemInfo) => {
isDone = true;
JSONArray tempArray = JSON.Parse(itemInfo) as JSONArray;
itemInfoJson = tempArray[0].AsObject;
};
StartCoroutine(Main.Instance.Web.GetItem(idlugar, getItemInfoCallback));
yield return new WaitUntil(() => isDone==true);
GameObject item = Instantiate(Resources.Load("Prefabs/item") as GameObject);
item.transform.SetParent(this.transform);
item.transform.localScale = Vector3.one;
item.transform.localPosition = Vector3.zero;
item.transform.Find("lugar").GetComponent<Text>().text = itemInfoJson["lugar"];
}
yield return null;
}
}
codigo Web
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
public class Web: MonoBehaviour {
void Start() {
//StartCoroutine(GetUsers());
// StartCoroutine(Login("testear","123456"));
//StartCoroutine(Registro("testear","123456"));
//StartCoroutine(GetItemsIDs());
}
//public void ShowUserItems(){
// StartCoroutine(GetItemsIDs(Main.Instance.Userinfo.Iduser));
// }
IEnumerator GetUsers() {
UnityWebRequest www = UnityWebRequest.Get("http://localhost/turismo/GetUsers.php");
yield return www.Send();
if(www.isNetworkError) {
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
// Or retrieve results as binary data
byte[] results = www.downloadHandler.data;
}
}
public IEnumerator Login(string login, string pass)
{
WWWForm form = new WWWForm();
form.AddField("nameuser", login);
form.AddField("passuser", pass);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/login.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
Main.Instance.Userinfo.SetCredentials(login, pass);
Main.Instance.Userinfo.SetID(www.downloadHandler.text);
if(www.downloadHandler.text.Contains("Wrong Credentials") || www.downloadHandler.text.Contains("no existe")){
Debug.Log("try agan");
}
else {
Main.Instance.UserProfile.SetActive(true);
Main.Instance.Loginn.gameObject.SetActive(false);
}
}
}
}
public IEnumerator Registro(string login, string pass)
{
WWWForm form = new WWWForm();
form.AddField("nameuser", login);
form.AddField("passuser", pass);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/registro.php", form))
{
yield return www.SendWebRequest();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log(www.downloadHandler.text);
}
}
}
public IEnumerator GetItem(string idlugar, System.Action<string> callback) {
WWWForm form = new WWWForm();
form.AddField("idlugar", idlugar);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItem.php", form))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError){
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
string jsonArray = www.downloadHandler.text;
callback(jsonArray);
}
}
}
public IEnumerator GetItemsIDs(string iduser, System.Action<string> callback) {
WWWForm form = new WWWForm();
form.AddField("iduser", iduser);
using (UnityWebRequest www = UnityWebRequest.Post("http://localhost/turismo/GetItemsIDs.php", form)){
yield return www.Send();
if (www.isNetworkError || www.isHttpError){
Debug.Log(www.error);
}
else {
// Show results as text
Debug.Log(www.downloadHandler.text);
string jsonArray = www.downloadHandler.text;
callback(jsonArray);
}
}
}
}
espero su ayuda