Search Unity

Screen space overlay for Dialogue stops Screenspace Overlay for HP bar from appearing.

Discussion in 'UGUI & TextMesh Pro' started by I-Like-Dogs, Aug 5, 2019.

  1. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9
    Hi all,

    I will apologise for the lack of detail right now ( I will add code and more detail when I get home from work).

    I am building an rpg, I have built a hp system and a dialogue system out of two separate screen space overlays, the hp system came first, but when i add the dialogue system it stops the hp bars (which follow the characters around) from appearing at all now. can these two overlays exist at the same time? I am new and have been relying heavily on guides found on popular video upload sites.

    I will add code when I'm home, I just wanted to get the question out there while its clear in my mind.

    thanks to all who read and especially all who would help.
     
  2. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class DialogueManager : MonoBehaviour
    7.  
    8.  
    9. {
    10.     public Text nameText;
    11.     public Text dialogueText;
    12.     public Animator animator;
    13.  
    14.     private Queue<string> sentences;
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         sentences = new Queue<string>();
    21.     }
    22.  
    23.     public void StartDialogue (Dialogue dialogue)
    24.     {
    25.  
    26.         animator.SetBool("isOpen", true);
    27.  
    28.         nameText.text = dialogue.name;
    29.  
    30.         sentences.Clear();
    31.  
    32.         foreach (string sentence in dialogue.sentences)
    33.  
    34.         {
    35.             sentences.Enqueue(sentence);
    36.  
    37.         }
    38.  
    39.         DisplayNextSentence();
    40.  
    41.     }
    42.  
    43.     public void DisplayNextSentence ()
    44.     {
    45.         if (sentences.Count == 0)
    46.         {
    47.             EndDialogue();
    48.             return;
    49.         }
    50.  
    51.         string sentence = sentences.Dequeue();
    52.         StopAllCoroutines();
    53.         StartCoroutine(TypeSentence(sentence));
    54.     }
    55.  
    56.     IEnumerator TypeSentence (string sentence)
    57.     {
    58.         dialogueText.text = "";
    59.         foreach (char letter in sentence.ToCharArray())
    60.         {
    61.             dialogueText.text += letter;
    62.             yield return null;
    63.         }
    64.     }
    65.  
    66.  
    67.         void EndDialogue()
    68.         {
    69.         animator.SetBool("isOpen", false);
    70.     }
    71.  
    72.  
    73.  
    74.  
    75. }
    76.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6.  
    7. [RequireComponent(typeof(CharacterStats))]
    8. public class HealthUI : MonoBehaviour
    9. {
    10.  
    11.  
    12. public GameObject uiPrefab;
    13. public Transform target;
    14. float visibleTime = 10;
    15.  
    16. float lastMadeVisibleTime;
    17.  
    18.  
    19.    Transform ui;
    20.     Image healthSlider;
    21.     Transform cam;
    22.  
    23.  
    24.  
    25.     //Start is called before the first frame updates
    26.     void Start()
    27.     {
    28.         cam = Camera.main.transform;
    29.      
    30.         foreach (Canvas c in FindObjectsOfType<Canvas>())
    31.         {
    32.             if (c.renderMode == RenderMode.WorldSpace)
    33.             {
    34.                 ui = Instantiate(uiPrefab, c.transform).transform;
    35.                 healthSlider = ui.GetChild(0).GetComponent<Image>();
    36.                 ui.gameObject.SetActive(false);
    37.                 break;
    38.             }
    39.         }
    40.  
    41.         GetComponent<CharacterStats>().OnHealthChanged += OnHealthChanged;
    42.     }
    43.    
    44.     void OnHealthChanged(int maxHealth, int currentHealth)
    45.         {
    46.         if (ui != null)
    47.         {
    48.             ui.gameObject.SetActive(true);
    49.             lastMadeVisibleTime = Time.time;
    50.        
    51.             float healthPercent = currentHealth / (float)maxHealth;
    52.             healthSlider.fillAmount = healthPercent;
    53.          
    54.             if (currentHealth <= 0)
    55.             {
    56.                 Destroy(ui.gameObject);
    57.             }
    58.         }
    59.  
    60.         }
    61.  
    the dialogue system runs on 3 separate scripts. I dont know why adding dialogue system broke health ui system. could anyone help me please?
     
  3. I-Like-Dogs

    I-Like-Dogs

    Joined:
    Jul 23, 2019
    Posts:
    9
    I've managed to fix this. seems that having 2 or 3 days away from the project allowed me to see clearly :)