Clear NPC Chatbox UI

Original Discord Post by petroslouca | 2024-10-01 20:45:04

Hello,

I am using the latest version of Convai SDK in Unity3D 2022.3.21f1 and I would like to clear the Chatbox dialogue text list completely.

There is a public function in ChatBoxUI.cs (attached to the Convai Transcript Canvas prefab that I am using), called ClearUI, see below, but calling it the dialogue is not cleared:

public void ClearUI()
{
foreach (Message message in _messageList) Destroy(message.TextObject.gameObject);
_messageList.Clear();
}

Could you please help me on that?

Also, is there a recommended way to disable the “thumbs up” icons/functionality in the chatbox completely?

Thank you!

Reply by k3kalinix | 2024-10-03 01:00:37

<@679221735458996224>

Reply by petroslouca | 2024-10-04 22:40:10

Hello! Any news regarding the issues mentioned? Thank you! <@1023671043287699568> <@679221735458996224>

Reply by k3kalinix | 2024-10-07 07:56:27

<@433610960725344267>

Reply by raghuvanshagarwal | 2024-10-07 08:47:42

Hi <@606256062613684224> sorry for late response can you try the following

 public void ClearUI()
        {
            for(int i = _messageList.Count - 1; i >= 0 ; i--){
            Destroy(messageList[i].TextObject.gameObject)
        }
            _messageList.Clear();
        }

Reply by k3kalinix | 2024-10-08 20:32:00

<@606256062613684224>

Reply by petroslouca | 2024-10-14 23:05:48

Thank you K3! I modified the code in ChatBoxUI.cs script as follows based on <@433610960725344267>'s suggestion:

///


/// Clears all messages from the UI.
///

public void ClearUI()
{
//foreach (Message message in _messageList) Destroy(message.TextObject.gameObject);
//_messageList.Clear();

        // Convai Discord suggested custom code for clearing UI (PL)
        for (int i = _messageList.Count - 1; i >= 0; i--)
        {
            Destroy(_messageList[i].TextObject.gameObject);
        }
        _messageList.Clear();
    }

I am calling the function as follows:

//Clear ChatUI from all messages
GameObject.Find(“Convai Transcript Canvas(Clone)”).GetComponent().ClearUI();

But the chat box is not cleared, am I missing something?

Reply by k3kalinix | 2024-10-14 23:06:51

GameObject.Find("Convai Transcript Canvas(Clone)").GetComponent<ChatBoxUI>().ClearUI();

Reply by k3kalinix | 2024-10-14 23:06:53

Dont use that

Reply by petroslouca | 2024-10-14 23:09:13

How should I call that function from another script?

Reply by k3kalinix | 2024-10-14 23:09:22

ChatBoxUI chatBoxUI = FindGameObjectsByType<ChatBoxUI>();
chatBoxUI.ClearUI();

Reply by k3kalinix | 2024-10-14 23:09:48

cache it

Reply by k3kalinix | 2024-10-14 23:09:57

then call it

Reply by petroslouca | 2024-10-14 23:10:37

Thank you, will try that and get back soon.

Reply by petroslouca | 2024-10-15 22:10:09

Unfortunately that did not work for me, I used the following function to iterate and destroy each chatbox message:

public void DestroyAllChatBoxUIMessages()
    {
        // Find the ChatBox's child GameObject "Content"
        GameObject chatBox = GameObject.Find("Content");

        if (chatBox != null)
        {
            // Loop through each child of the ChatBox
            foreach (Transform child in chatBox.transform)
            {
                // Check if the child's name is "ChatBox(Clone)"
                if (child.gameObject.name == "ChatBox(Clone)")
                {
                    // Destroy the child GameObject
                    Destroy(child.gameObject);
                }
            }

            Debug.Log("All ChatBox(Clone) gameobjects destroyed!");
        }
        else
        {
            Debug.LogWarning("ChatBox(Clone) GameObject not found.");
        }
    }

Reply by k3kalinix | 2024-10-15 23:03:59

Is it working with this code?

Reply by petroslouca | 2024-10-16 11:45:29

Yes, each chatbox message under Content is destroyed, therefore clearing all UI messages.

Reply by k3kalinix | 2024-10-16 12:00:02

Awesome!

This conversation happened on the Convai Discord Server, so this post will be closed.