简体   繁体   中英

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. 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. Ensure that your URL ( https://PROJECT_URL.firebaseio.com/brokenComp.json ) is correct.

First of all the HTTP error 404 means a provided URL was not found. This has nothing to do with the static field.


As we don't see where/if you assign a value to TextDisplay.Message it might always be null . (You also confirmed this using a breakpoint.) I would at least initialize it with an empty string to avoid exceptions.

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.

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

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM