Home > Net >  update function does not go through all the line of codes inside it - Unity
update function does not go through all the line of codes inside it - Unity

Time:12-05

I have a problem where the line of codes in my Update function does not work. I added a debug.log both at the start and end of the update function but the whole if statement does not work. The dialogCycle variable does not even increment. Is there something wrong with the whole script?

Here is the script:

using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class TelevisionEvent : MonoBehaviour
{
    [SerializeField] private GameData gameData; // SCRIPTABLEOBJECT

    [SerializeField] private GameObject dialogbox, interactButton;
    [SerializeField] private FollowPlayer followPlayer;
    [SerializeField] private Transform tv, player;

    [SerializeField] private TextAsset objectData;
    private ObjectDialog objectJson;

    public string[] dialogMessage;
    public int dialogCycle;
    public bool clickEnable;

    private void Update()
    {
        //if (gameData.isWatched) return;
        if (Input.GetMouseButtonDown(0) && clickEnable)
        {
            Debug.Log("Clicked"); // THIS WORKS

            dialogCycle  ; // I PUT THIS OUTSIDE TO CHECK IF IT WILL WORK OUTSIDE THE IF BUT IT STILL DOES NOT
            tvDialog();    // THE DEBUG.LOG IS THE ONE THAT ONLY WORKS
            if (dialogCycle != dialogMessage.Length - 1) // BUT THIS WHOLE IF STATEMENT DOES NOT GET CHECKED
            {
                dialogCycle  ;
                tvDialog();
            }
            else
            {
                dialogbox.SetActive(false);
                dialogCycle = 0;
                Array.Clear(dialogMessage, 0, dialogMessage.Length);
                clickEnable = false;
                gameData.isWatched = true;
                followPlayer.player = player;
            }
            Debug.Log("Clicked2"); // THIS WORKS TOO
        }
    }

    private void OnTriggerEnter(Collider collisionInfo)
    {
        if(gameData.isWatched) return;
        if (collisionInfo.CompareTag("Player"))
        {
            interactButton.GetComponent<Button>().onClick.RemoveListener(tvDialog);
            interactButton.GetComponent<Button>().onClick.AddListener(tvDialog);
        }
    }

    private void OnTriggerExit(Collider collisionInfo)
    {
        Debug.Log("Hello");
        if (gameData.isWatched) return;
        if (collisionInfo.CompareTag("Player"))
        {
            interactButton.GetComponent<Button>().onClick.RemoveListener(tvDialog);
        }
    }

    public void tvDialog()
    {
        dialogbox.SetActive(true);
        interactButton.SetActive(false);
        dialogCycle = 0;

        followPlayer.player = tv;

        objectJson = JsonUtility.FromJson<ObjectDialog>(objectData.text);
        loadDialog();
    }

    public void loadDialog()
    {
        dialogbox.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = objectJson.name;
        dialogMessage = objectJson.object_dialog.Split('#');

        dialogbox.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = dialogMessage[dialogCycle];
        clickEnable = true;
    }
}

For more info, the game object that it is attached to have another script. Will it have an effect in this script? the other script is just like this but it is like an object interaction manager for the map (opening doors, etc.).

inspector

By 'just like this' I mean there is also a line of codes similar to the prior script above. The script above is for an event when the player opens the door for the first time a television dialogue event will trigger. In the other script, I have the object interaction where I interact with object like a radio and the dialogue will show.

I can just show you the script just for more info. the similarities will be in the update function and the last part of the script which is the objectDialog method

using System;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class ObjectInteraction : MonoBehaviour
{
    [SerializeField] private GameData gameData;

    [SerializeField] private GameObject interactButton, cafeArrow, aptArrow, konbiniArrow;
    [SerializeField] private GameObject cryptogram;
    [SerializeField] private GameObject dialogbox;

    private static ObjectInteraction instance;

    private ObjectDialog objectJson;
    private TextAsset objectData;
    private string[] dialogMessage;
    private int dialogCycle;
    private bool clickEnable;

    private int[] apartment = { 3, 8, 15, 16 }; // APARTMENT
    private int[] cafe = Enumerable.Range(0, 17).ToArray(); // CAFE
    private int[] konbini = { 77, 78, 82, 83 }; // CONVENIENT STORE

    private static bool isInside, isOpen;

    private void Start()
    {
        isInside = false;
        isOpen = false;
        //Debug.Log(dialogCycle);
    }

    private void Update()
    {
        //if (instance != this) return;

        if (Input.GetMouseButtonDown(0) && clickEnable)
        {
            if(dialogCycle != dialogMessage.Length-1)
            {
                dialogCycle  ;
                loadDialog();
            }else
            {
                dialogbox.SetActive(false);
                dialogCycle = 0;
                Array.Clear(dialogMessage, 0, dialogMessage.Length);
                clickEnable = false;
            }
        }
    }

    private void OnTriggerEnter(Collider collisionInfo)
    {
        if(collisionInfo.CompareTag("Player"))
        {
            if(gameObject.CompareTag("Door"))
            {
                interactButton.SetActive(true);
                interactButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("InteractionAsset/OBJECT");
                interactButton.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = "Open";

                interactButton.GetComponent<Button>().onClick.RemoveListener(enter);
                interactButton.GetComponent<Button>().onClick.AddListener(enter);
            }
            else if(gameObject.CompareTag("Newspaper"))
            {
                if (!gameData.cryptoDone)
                {
                    interactButton.SetActive(true);
                    interactButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("InteractionAsset/OBJECT");
                    interactButton.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = "View";
                    gameObject.GetComponent<Outline>().enabled = true;

                    // ADD ONCLICK FOR NEWSPAPER
                    interactButton.GetComponent<Button>().onClick.RemoveAllListeners();
                    interactButton.GetComponent<Button>().onClick.AddListener(showCryptogram);
                }
            }
            else if(gameObject.CompareTag("ObjectDialog"))
            {
                interactButton.SetActive(true);
                interactButton.GetComponent<Image>().sprite = Resources.Load<Sprite>("InteractionAsset/OBJECT");
                interactButton.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = "View";
                gameObject.GetComponent<Outline>().enabled = true;

                interactButton.GetComponent<Button>().onClick.RemoveAllListeners();
                interactButton.GetComponent<Button>().onClick.AddListener(objectDialog);
            }
            
            // WHEN LEAVING ROOMS
            if (gameObject.name == "apt_floor")
            {
                isInside = true;
                aptArrow.SetActive(true);
            }
            else if (gameObject.name == "cafe_floor")
            {
                isInside = true;
                cafeArrow.SetActive(true);
            }
            else if(gameObject.name == "konbini_floor")
            {
                isInside = true;
                konbiniArrow.SetActive(true);
            }
        }
    }

    private void OnTriggerExit(Collider collisionInfo)
    {
        interactButton.SetActive(false);
        interactButton.GetComponent<Button>().onClick.RemoveAllListeners();

        if (collisionInfo.CompareTag("Player"))
        {
            // TURN OFF OUTLINE / HIGHLIGHT 
            if(gameObject.CompareTag("Newspaper"))
            {
                gameObject.GetComponent<Outline>().enabled = false;
            }
            else if (gameObject.CompareTag("ObjectDialog"))
            {
                gameObject.GetComponent<Outline>().enabled = false;
            }

            // WHEN LEAVING ROOMS
            if (gameObject.name == "apt_floor" || (gameObject.name == "apt_wall" && !isInside && isOpen))
            {
                for (int i = 0; i < apartment.Length; i  )
                {
                    transform.root.GetChild(apartment[i]).GetComponent<MeshRenderer>().enabled = true;
                    transform.root.GetChild(apartment[i]).gameObject.SetActive(true);
                }
                aptArrow.SetActive(false);
            }
            else if(gameObject.name == "cafe_floor" || (gameObject.name == "cafe_wall" && !isInside && isOpen))
            {
                
                for (int i = 0; i < cafe.Length; i  )
                {
                    if (i != 13)
                    {
                        transform.root.GetChild(cafe[i]).gameObject.SetActive(true);
                    }
                }
                transform.root.GetChild(63).GetComponent<MeshRenderer>().enabled = true;
                transform.root.GetChild(67).GetComponent<MeshRenderer>().enabled = true;
                transform.root.GetChild(68).GetComponent<MeshRenderer>().enabled = true;
                transform.root.GetChild(59).gameObject.SetActive(true);
                cafeArrow.SetActive(false);
            }
            else if(gameObject.name == "konbini_floor" || (gameObject.name == "konbini_wall" && !isInside && isOpen))
            {
                for (int i = 0; i < konbini.Length; i  )
                {
                    transform.root.GetChild(konbini[i]).GetComponent<MeshRenderer>().enabled = true;
                }
                transform.root.GetChild(73).gameObject.SetActive(true);
                transform.root.GetChild(74).gameObject.SetActive(true);
                konbiniArrow.SetActive(false);  
            }
        }
    }

    // ENTER ROOM
    public void enter()
    {
        FindObjectOfType<AudioManager>().Play("ButtonSound");
        if (gameObject.name == "apt_door")
        {
            for(int i = 0; i < apartment.Length; i  )
            {
                transform.root.GetChild(apartment[i]).GetComponent<MeshRenderer>().enabled = false;
            }
            gameObject.SetActive(false);
        }else if(gameObject.name == "cafe_door")
        {
            for (int i = 0; i < cafe.Length; i  )
            {
                if(i != 13)
                {
                    transform.root.GetChild(cafe[i]).gameObject.SetActive(false);
                }
            }
            transform.root.GetChild(63).GetComponent<MeshRenderer>().enabled = false;
            transform.root.GetChild(67).GetComponent<MeshRenderer>().enabled = false;
            transform.root.GetChild(68).GetComponent<MeshRenderer>().enabled = false;
            gameObject.SetActive(false);
        }else if(gameObject.name == "konbini_door1")
        {
            for (int i = 0; i < konbini.Length; i  )
            {
                transform.root.GetChild(konbini[i]).GetComponent<MeshRenderer>().enabled = false;
            }
            transform.root.GetChild(73).gameObject.SetActive(false);
            transform.root.GetChild(74).gameObject.SetActive(false);
        }
        isOpen = true;
        interactButton.SetActive(false);
        interactButton.GetComponent<Button>().onClick.RemoveListener(enter);
    }

    // SHOW CRYPTOGRAM MINIGAME IN OBJECT
    public void showCryptogram()
    {
        FindObjectOfType<AudioManager>().Play("ButtonSound");
        cryptogram.SetActive(true);
    }

    public void objectDialog()
    {
        dialogbox.SetActive(true);
        interactButton.SetActive(false);
        dialogCycle = 0;

        loadJson();
        loadDialog();
    }
    
    public void loadJson()
    {
        if (gameObject.name == "Radio")
        {
            objectData = Resources.Load<TextAsset>("JSON/Objects/Radio");
        }else if(gameObject.name == "Television")
        {
            objectData = Resources.Load<TextAsset>("JSON/Objects/TV");
        }
        objectJson = JsonUtility.FromJson<ObjectDialog>(objectData.text);
    }
    
    public void loadDialog()
    {
        dialogbox.transform.GetChild(0).GetComponent<TextMeshProUGUI>().text = objectJson.name;
        dialogMessage = objectJson.object_dialog.Split('#');

        if (dialogMessage.Length == 1)
        {
            dialogbox.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = objectJson.object_dialog;
        }
        else if (dialogCycle < dialogMessage.Length)
        {
            dialogbox.transform.GetChild(1).GetComponent<TextMeshProUGUI>().text = dialogMessage[dialogCycle];
            clickEnable = true;
        }
    }
}

CodePudding user response:

You set dialogCycle to 0 in tvDialog() after incrementing it, and in the else statement so it will never increment.

CodePudding user response:

Try to attach debugger and put breakpoint where you are trying to increment. Inspect variable and make some steps over and inspect again, try to press "Continue" and reach that breakpoint again. I assume there's something that assigns 0 to dialogCycle. It can't be otherwise because it is impossible that this operation just ignored

  • Related