Forum rules - please read before posting.

Add a custom image for different alternatives in a Conversation

Hi.
I'm trying to create a script that changes different images and labels in a conversation, the idea is to add a personalized icon depending on the type of answer that we have. With this in mind I created the script making the conversation change the image, however, if there's more than 1 conversation in a single scene then all the conversations will have the same images and labels (even though I defined different images and labels in each one of them) I tried to add a way to compare if the conversation occurring at the moment is the same that the one referenced in the script. It worked well only in the first conversation that ever happens in the game, the next one gives a null reference exception saying that I'm trying to look for a component that is actually destroyed.

Here is the script that I'm actually using

using UnityEngine;
using AC;
using System;

public class DialogueOptionSounds : MonoBehaviour
{


    [SerializeField] private DialogueOptionImage[] dialogueOptionImages = new DialogueOptionImage[4];

    private void OnEnable() { EventManager.OnMenuTurnOn += OnMenuTurOn; EventManager.OnMenuElementShow += OnMenuElementShow;}


    private void OnMenuElementShow(MenuElement menuElement)
    {
        if(menuElement.name == "Kid" || menuElement.name == "Grown")
        {
            Debug.LogWarning("Do");
        }
    }

    private void OnMenuTurOn(Menu _menu, bool isInstant)
    {
        if (_menu.appearType == AppearType.DuringConversation)
        {
            if(gameObject != null)
            {
                if (gameObject.GetComponent<Conversation>() == Conversation.CurrentConversation)
                {
                    if (gameObject.GetComponent<Conversation>() != null)
                    {
                        Debug.Log("MenuDeConversacion");
                        MenuGraphic alternative_1 = (MenuGraphic)_menu.GetElementWithName("Atacar_1");
                        MenuGraphic alternative_2 = (MenuGraphic)_menu.GetElementWithName("Atacar_2");
                        MenuGraphic alternative_3 = (MenuGraphic)_menu.GetElementWithName("Atacar_3");
                        MenuGraphic alternative_4 = (MenuGraphic)_menu.GetElementWithName("Atacar_4");

                        MenuLabel label_1 = (MenuLabel)_menu.GetElementWithName("Atacar_1_Label");
                        MenuLabel label_2 = (MenuLabel)_menu.GetElementWithName("Atacar_2_Label");
                        MenuLabel label_3 = (MenuLabel)_menu.GetElementWithName("Atacar_3_Label");
                        MenuLabel label_4 = (MenuLabel)_menu.GetElementWithName("Atacar_4_Label");

                        alternative_1.IsVisible = false;
                        alternative_2.IsVisible = false;
                        alternative_3.IsVisible = false;
                        alternative_4.IsVisible = false;

                        label_1.IsVisible = false;
                        label_2.IsVisible = false;
                        label_3.IsVisible = false;
                        label_4.IsVisible = false;

                        MenuDialogList dialogList = (MenuDialogList)_menu.GetElementWithName("DialogueList");


                        for (int i = 0; i < dialogList.GetNumSlots(); i++)
                        {

                            switch (i)
                            {
                                case 0:
                                    alternative_1.IsVisible = true;
                                    label_1.IsVisible = true;
                                    alternative_1.SetNormalGraphicTexture(dialogueOptionImages[0].optionImage);
                                    label_1.label = dialogueOptionImages[0].description;
                                    break;
                                case 1:
                                    alternative_2.IsVisible = true;
                                    label_2.IsVisible = true;
                                    alternative_2.SetNormalGraphicTexture(dialogueOptionImages[1].optionImage);
                                    label_2.label = dialogueOptionImages[1].description;
                                    break;
                                case 2:
                                    alternative_3.IsVisible = true;
                                    label_3.IsVisible = true;
                                    alternative_3.SetNormalGraphicTexture(dialogueOptionImages[2].optionImage);
                                    label_3.label = dialogueOptionImages[2].description;
                                    break;
                                case 3:
                                    alternative_4.IsVisible = true;
                                    label_4.IsVisible = true;
                                    alternative_4.SetNormalGraphicTexture(dialogueOptionImages[3].optionImage);
                                    label_4.label = dialogueOptionImages[3].description;
                                    break;
                            }
                        }
                    }
                    else
                    {

                        Debug.LogError("NO tiene conversacion");
                    }
                }
                else
                {
                    Debug.LogError("NO son la misma conversacion");
                }
            }
            else
            {
                Debug.LogError("NO hay objeto");
            }
        }
        else
        {
            Debug.LogError("NO es una conversacion");
        }
    }



    [System.Serializable]
    private class DialogueOptionImage
    {
        public string description;
        public Texture optionImage;

    }


}

Comments

  • edited December 2021

    Is an instance of this script placed on each Conversation object?

    If you wish to run some custom code when a specific Conversation is run, you can use the OnStartConversation event and compare its parameter:

    using UnityEngine;
    using AC;
    
    public class ConversationEventExample : MonoBehaviour
    {
    
        void OnEnable () { EventManager.OnStartConversation += OnStartConversation; }
    
        void OnDisable () { EventManager.OnStartConversation -= OnStartConversation; }
    
        void OnStartConversation (Conversation conversation)
        {
            if (conversation.gameObject == gameObject)
            {
                // Conversation on this object was run
            }
        }
    
    }
    
  • Yea that’s what I wanted. The script is placed in each conversation since all conversations have these images and labels. Thanks to let me know what parameter I should look… I will try this solution and see how it goes
  • Hi. In the end, it didn't help me too much since the problem itself is the game object being null. Also, I need to access the menu elements in the conversation. However, I approached the problem with another perspective. What I've done to solve it is using the unique dilemma ID from the DB (That's how we call the conversation in our game) and comparing it with a GlobalVariable that I change before each conversation with the corresponding ID.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.