0

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

4
  • Sure you dont have 2 copies of the script with one not having the ode object assigned in the inspector Commented Apr 14, 2023 at 11:25
  • 1
    Well, it turns out that I'm a big dumb dumb and forgot to double-check that I had 2 copies of the script. Commented Apr 14, 2023 at 11:30
  • Hey. Least you have some humour and wear it with pride. :) enjoy Commented Apr 14, 2023 at 11:54
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Commented Apr 17, 2023 at 8:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.