SOLVED - 2 copies of the script in unity
I'm working on UI that gets information from a web socket. I have run into a problem where code_object (and code_text) keeps switching from being assigned and null.
(it also causes an error:
UnassignedReferenceException: The variable code_object of WsClient has not been assigned. You probably need to assign the code_object variable of the WsClient script in the inspector. UnityEngine.GameObject.GetComponent[T] () (at <05041d4f5ec242309356a6b3b04452e6>:0) WsClient.Start () (at Assets/scripts/WsClient.cs:20)
because I try to use it while being "null")
I have no idea what could cause the problem, I may have made a stupid but I have no clue.
Debug output debug code
private void Update()
{if (code_object != null) { Debug.Log("not null"); }
if (code_object == null) { Debug.Log("null"); }
}
All code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WebSocketSharp;
using TMPro;
public class WsClient : MonoBehaviour
{
public string code;
public string message;
public GameObject code_object;
TextMeshProUGUI code_text;
private WebSocket ws;
private void Start()
{
ws = new WebSocket("ws://localhost:8080");
ws.OnMessage += OnMessage;
ws.Connect();
code_text = code_object.GetComponent<TextMeshProUGUI>();
Debug.Log("code text =" + code_text);
Debug.Log("code object =" + code_object);
}
private void OnMessage(object sender, MessageEventArgs e)
{
Debug.Log("Message Received from " + ((WebSocket)sender).Url + ", Data : " + e.Data);
if (e.Data.Contains("Your code is:"))
{
Debug.Log("Code contains 'Your code is:'");
code = e.Data.Substring("Your code is:".Length);
Debug.Log(code);
//code_text = code_object.GetComponent<TextMeshProUGUI>();
Debug.Log("code text =" + code_text);
Debug.Log("test");
Debug.Log("code object =" + code_object);
Debug.Log("test2");
if (code_text != null && code != null)
{
Debug.Log("'code_text != null && code != null' is true");
Debug.Log(code);
code_text.text = code;
}
}
else
{Debug.Log("Code contains 'Your code is:'");
Debug.Log("Code does not contain 'Your code is:'");
}
}
private void Update()
{if (code_object != null) { Debug.Log("not null"); }
if (code_object == null) { Debug.Log("null"); }
//Debug.Log("code object =" + code_object);
//Debug.Log(code);
/*if (code_text != null && code != null)
{
Debug.Log("'code_text != null && code != null' is true");
code_text.text = code;
}*/
}
public void startserver()
{
if (ws != null)
{
ws.Send("CL");
}
}
}
I tried to update the UI but it seems that the error results in nothing happening