[英]Accessing static member to another class not working?
I am coming to an issue where I am trying to access a static member to get that message to show in my database when the user selects that particular object.我遇到了一个问题,当用户选择特定的 object 时,我试图访问 static 成员以使该消息显示在我的数据库中。 By the way, my front end I am using its in unity.顺便说一句,我的前端我正在统一使用它。 So, can anyone help me solve this issue.那么,谁能帮我解决这个问题。 thanks for the help.谢谢您的帮助。
here is my code:这是我的代码:
public void NextButton()
{
if (highlightSet == true)
{
var httpWebRequest =
(HttpWebRequest) WebRequest.Create("https://PROJECT_URL.firebaseio.com/brokenComp.json");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "PUT";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string missingObjectCount = TextDisplay.Message;
streamWriter.Write(missingObjectCount);
}
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
TextDisplay:文本显示:
public class TextDisplay: MonoBehaviour
{
public static string Message;
}
updated codee更新的代码
public void StartUpload()
{
StartCoroutine(Upload());
}
public void NextButton()
{
if (highlightSet == true)
{
IEnumerator Upload()
{
byte[] myData = System.Text.Encoding.UTF8.GetBytes(TextDisplay.Message);
using (UnityWebRequest www = UnityWebRequest.Put("https://PROJECT_URL.firebaseio.com/brokenComp.json", myData))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Upload complete!");
}
}
}
If you're getting a 404 error from your web request, its because Firebase can't locate the resource you've requested.如果您从 web 请求中收到 404 错误,那是因为 Firebase 无法找到您请求的资源。 Ensure that your URL ( https://PROJECT_URL.firebaseio.com/brokenComp.json
) is correct.确保您的 URL ( https://PROJECT_URL.firebaseio.com/brokenComp.json
) 是正确的。
First of all the HTTP error 404 means a provided URL was not found.首先HTTP 错误 404表示未找到提供的 URL。 This has nothing to do with the static
field.这与static
字段无关。
As we don't see where/if you assign a value to TextDisplay.Message
it might always be null
.由于我们看不到/如果您为TextDisplay.Message
分配值,它可能始终是null
。 (You also confirmed this using a breakpoint.) I would at least initialize it with an empty string
to avoid exceptions. (您还使用断点确认了这一点。)我至少会使用空string
对其进行初始化以避免异常。
public static string Message;
Since you asked for it:既然你要求它:
As said in the comments I woukd rather use UnityWebRequest.Put
to not block the entire thread until the request is done.正如评论中所说,我宁愿使用UnityWebRequest.Put
在请求完成之前不阻塞整个线程。
This is just the example (with small edits) from the API but I guess it js pretty straight forward and you should be able to use it for your purpose这只是 API 中的示例(稍作修改),但我想它非常简单,您应该可以将它用于您的目的
public class MyBehavior : MonoBehaviour
{
public void NextButton()
{
StartCoroutine(Upload());
}
IEnumerator Upload()
{
byte[] myData = System.Text.Encoding.UTF8.GetBytes(TextDisplay.Message);
using (UnityWebRequest www = UnityWebRequest.Put(YOUR_URL, myData))
{
yield return www.Send();
if (www.isNetworkError || www.isHttpError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Upload complete!");
}
}
}
}
Btw is something hindering you from.using the Firebase SDK ?顺便说一句,有什么阻碍你使用Firebase SDK吗?
There it would be something like会有类似的东西
public class MyScript: MonoBehaviour {
void Start() {
// Set up the Editor before calling into the realtime database.
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://YOUR-FIREBASE-APP.firebaseio.com/");
// Get the root reference location of the database.
reference = FirebaseDatabase.DefaultInstance.RootReference;
}
public void NextButton()
{
// ported the variable names from your latest question
mDatabaseRef.Child("messages").Child("message").SetValueAsync(TextDisplay.Message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.