2012-04-13 345 views
1

嗨,大家好我是新來的C#和untiy我通常開發Java,並有一個問題,使用IEnumerator返回一個值。基本上我有一個客戶端腳本,它將用戶名和密碼註冊到服務器上的數據庫中。我的問題是,當我點擊一個按鈕時,應調用客戶端腳本中的方法,將用戶輸入的用戶名和密碼添加到數據庫中。負責這個的方法應該返回一個字符串,表明註冊成功/不成功。當我嘗試實現這個我得到以下錯誤:返回一個值IEnumerator

「的NullReferenceException:未設置爲一個對象 RegisterScript.OnMouseUp的實例對象引用()(在資產/腳本/ RegisterScript.cs:23) UnityEngine。 SendMouseEvents:DoSendMouseEvents()「

以下是兩個類的代碼,感謝任何人可以幫助!附接到通過用戶名和密碼的客戶端腳本

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); 
    } 
    } 
+1

從這個上面的代碼,我沒有看到任何可以被IEnumerable的。 – 2012-04-13 12:28:02

+0

相關,並有更好的解決方案imho:http://answers.unity3d.com/questions/207733/can-coroutines-return-a-value.html – cregox 2012-09-26 16:08:30

回答

0

FindObjectOfType將返回null如果YOUT沒有該類型的GameObject(在你的案例ClientSideScript)在你的unity3d場景中。

你還需要改變你的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電話響應。

而當你從RegisterScriptRegisterUsers你需要使用StartCoroutine(client.RegisterUsers(name, pass));

看到Unity3d WWW documentationCoroutine documentation

+0

嗯?這是如何回答這個問題的?我仍然無法用此回報價值!你只是在最後調用一個方法! – cregox 2012-09-26 16:08:04

1

FindObjectOfType函數返回一個空因某種原因按鈕

客戶端腳本

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; 
} 
} 

註冊腳本。 爲什麼僅僅根據提供的代碼來推導是不可能的。

IEnumerable<string> stringcheck = client...,但在此之前(即使我沒有看到提供的文本行號)線

+0

我應該用什麼來代替IEnumberable? – racingWild 2012-04-13 12:30:31

+0

只是一個字符串 - 你總是返回1個字符串。 – 2012-04-13 12:31:17

+0

@racingWild:我再說一遍,這不是關於您的ClientSideScript對象恢復的可枚舉bu(儘可能多的理解所提供的代碼) – Tigran 2012-04-13 12:31:36

0

我碰到過這樣的問題太多。

1.我想把輸出變量放在參數列表中。

2.所以我想指針,但統一3d使用安全的C#沒有指針。

3.然後我想在c#中的「ref」或「out」,但函數返回IEnumerator不能在c#中的參數列表中使用「ref」或「out」。

4.Finial我想我可以寫一個指針我自己在安全的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; 
    }