C# 返回值IEnumerator

C# 返回值IEnumerator,c#,unity3d,ienumerable,C#,Unity3d,Ienumerable,嘿,伙计们,我是c#新手,untiy我通常开发Java,使用IEnumerator返回值时遇到问题。基本上,我有一个客户端脚本,它向服务器上的数据库注册用户名和密码。我的问题是,当我单击按钮时,应该调用客户端脚本中的方法,将用户输入的用户名和密码添加到数据库中。负责此操作的方法应返回一个字符串,指示注册成功/不成功。当我尝试实现此功能时,会出现以下错误: “NullReferenceException:对象引用未设置为对象的实例 RegisterScript.OnMouseUp()(位于Asse

嘿,伙计们,我是c#新手,untiy我通常开发Java,使用IEnumerator返回值时遇到问题。基本上,我有一个客户端脚本,它向服务器上的数据库注册用户名和密码。我的问题是,当我单击按钮时,应该调用客户端脚本中的方法,将用户输入的用户名和密码添加到数据库中。负责此操作的方法应返回一个字符串,指示注册成功/不成功。当我尝试实现此功能时,会出现以下错误:

“NullReferenceException:对象引用未设置为对象的实例 RegisterScript.OnMouseUp()(位于Assets/Scripts/RegisterScript.cs:23) UnityEngine.SendMouseEvents:DoSendMouseEvents()

下面是这两个类的代码,感谢所有能够提供帮助的人

客户端脚本

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ClientSideScript : MonoBehaviour {

public string RegUserUrl = "http://localhost/reguser.php?";


public IEnumerable<string> RegisterUsers(string name , string pass){

    string post_url = registerUser + "name=" + name + "&pass=" + pass;
    print (post_url);
    WWW hs_post = new WWW(post_url);
    string check = hs_post.ToString();
    yield return check;

    //return regname;
 }
 }
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RegisterScript : MonoBehaviour {
public string Registeredpassword;
public string Registeredusername;
public Texture2D registerButton;



//Use this for initialization
void OnGUI () {

Registeredpassword = GUILayout.PasswordField(Registeredpassword, "*"[0], 25);
Registeredusername = GUILayout.TextField(Registeredusername, 25);

}

void OnMouseUp(){

ClientSideScript client =          (ClientSideScript)FindObjectOfType(typeof(ClientSideScript));
 IEnumerable<string> stringcheck = client.RegisterUsers(Registeredusername ,      Registeredpassword);
 print (stringcheck);
    }   
  }
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类客户端脚本:MonoBehavior{
公共字符串调节器URL=”http://localhost/reguser.php?";
公共IEnumerable注册表用户(字符串名称、字符串传递){
字符串post_url=registerUser+“name=“+name+”&pass=“+pass;
打印(post_url);
WWW hs_post=新的WWW(post_url);
字符串检查=hs_post.ToString();
退货检查;
//返回regname;
}
}
附加到按钮的注册脚本,该按钮将用户名和密码传递给客户端脚本

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class ClientSideScript : MonoBehaviour {

public string RegUserUrl = "http://localhost/reguser.php?";


public IEnumerable<string> RegisterUsers(string name , string pass){

    string post_url = registerUser + "name=" + name + "&pass=" + pass;
    print (post_url);
    WWW hs_post = new WWW(post_url);
    string check = hs_post.ToString();
    yield return check;

    //return regname;
 }
 }
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RegisterScript : MonoBehaviour {
public string Registeredpassword;
public string Registeredusername;
public Texture2D registerButton;



//Use this for initialization
void OnGUI () {

Registeredpassword = GUILayout.PasswordField(Registeredpassword, "*"[0], 25);
Registeredusername = GUILayout.TextField(Registeredusername, 25);

}

void OnMouseUp(){

ClientSideScript client =          (ClientSideScript)FindObjectOfType(typeof(ClientSideScript));
 IEnumerable<string> stringcheck = client.RegisterUsers(Registeredusername ,      Registeredpassword);
 print (stringcheck);
    }   
  }
使用UnityEngine;
使用系统集合;
使用System.Collections.Generic;
公共类RegisterScript:MonoBehavior{
公共字符串注册密码;
公共字符串注册器名称;
公共纹理2D寄存器按钮;
//用于初始化
void OnGUI(){
Registeredpassword=GUILayout.PasswordField(Registeredpassword,“*”[0],25);
Registeredusername=GUILayout.TextField(Registeredusername,25);
}
void OnMouseUp(){
ClientSideScript客户端=(ClientSideScript)FindObjectOfType(typeof(ClientSideScript));
IEnumerable stringcheck=client.RegisterUsers(Registeredusername、Registeredpassword);
打印(stringcheck);
}   
}

您的
FindObjectOfType
函数由于某种原因返回空值。 为什么,仅仅看提供的代码是不可能扣除的


它不是
IEnumerable stringcheck=client…
,而是前面的一行(即使我在提供的文本中没有看到行号)

如果unity3d场景中没有该类型的
GameObject
(在您的例子中是
ClientSideScript
),FindObjectOfType将返回null

您还需要将
RegisterUsers
方法更改为类似以下内容

public IEnumerator RegisterUsers(string name , string pass){

string post_url = registerUser + "name=" + name + "&pass=" + pass;
print (post_url);
WWW hs_post = new WWW(post_url);

// wait for www to get a response
yield return hs_post;

// now do something with the returned value
// cant return it directly as this is a unity3d coroutine
string response = hs_post.text;
DealWithResponse(reponse)
}

它有一个
IEnumerator
返回类型,因为它需要一个统一的协同程序,这样游戏就不会等待对www呼叫的响应

当您从
RegisterScript
调用
RegisterUsers
时,需要使用
start例程(client.RegisterUsers(name,pass))


参见Unity3d和

我也遇到了这个问题

1.我考虑将输出变量放入参数列表中

2.所以我考虑使用指针,但unity 3d使用安全的c#,它没有指针

3.然后我考虑c#中的“ref”或“out”,但函数返回IEnumerator不能在c#中的参数列表中使用“ref”或“out”

4.最后,我想我可以自己在安全的c#中用通用的东西写一个指针

以下是我的解决方案:

//a pointer to another object..
public class Pointer<T>
{
    public T data;

    public Pointer(){
    }
    public Pointer(T data){
        this.data = data;
    }
}

  public IEnumerator RegisterUsers(string name , string pass,Pointer<string> errPtr){
    string post_url = registerUser + "name=" + name + "&pass=" + pass;
    print (post_url);
    WWW hs_post = new WWW(post_url);

    // wait for www to get a response
    yield return hs_post;

    //return it in parameter
    errPrt.data = hs_post.text;
  }
//指向另一个对象的指针。。
公共类指针
{
公共数据;
公共指针(){
}
公共指针(T数据){
这个数据=数据;
}
}
公共IEnumerator注册表用户(字符串名称、字符串传递、指针errPtr){
字符串post_url=registerUser+“name=“+name+”&pass=“+pass;
打印(post_url);
WWW hs_post=新的WWW(post_url);
//等待www获得响应
收益率回报率;
//在参数中返回它
errPrt.data=hs_post.text;
}

从上面的代码中,我看不到任何与IEnumerable.related相关的内容,如果有更好的解决方案,我该用什么来代替IEnumerable?只有一个字符串-您总是返回一个字符串。@racingWild:我重复一遍,这不是关于可枚举的,而是关于ClientSideScript对象恢复的(尽可能理解提供的代码)@racingWild:查看stacktrace,至少在这里提供的stacktrace上,我看到的最后一点不是RegisterUsers方法,而是OnMouseUp。@JakubKonecki我厌倦了只使用字符串,但它给出了一个错误,说它不是迭代器块。嗯?这是如何回答这个问题的?我仍然不能用它返回值!你只是调用了一个方法I最后!!