Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question OnTriggerEnter/Exit Collider2d Dialogbox problem

Discussion in 'Scripting' started by zeus128, Sep 18, 2020.

  1. zeus128

    zeus128

    Joined:
    Jun 14, 2020
    Posts:
    3
    Hey Everyone im new to C# and Unity game devoloping. İm trying to make a simple Dialogue system. When i enter Collider2D is set trigger Dialogue appears, when i Exit Dialogue disapper. İ do somewhat close to this. But not work as neded. When i enter collider Dialog box continously activated and disactivated even if i dont exit the collider. Can Someone tweak code a bit ? And Add V2 as a Destroy game object line on exit proper way ? For on the exit disabiling the box. İ cant manage the do that in working way.Here's the code lines;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class DialogueSysteam : MonoBehaviour {
    8.  
    9.     public GameObject dialogBox;
    10.     public TMP_Text dialogText;
    11.     public string dialog;
    12.     public bool playerInRange;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(playerInRange)
    24.         {
    25.  
    26.      
    27.              if(dialogBox.activeInHierarchy){
    28.               dialogBox.SetActive(false);
    29.              } else{
    30.               dialogBox.SetActive(true);
    31.               dialogText.text = dialog;
    32.              }
    33.         }
    34.     }
    35.     private void OnTriggerEnter2D(Collider2D other)
    36.     {
    37.         if (other.CompareTag("Player"))
    38.         {
    39.             playerInRange = true;
    40.         }
    41.     }
    42.  
    43.     private void OnTriggerExit2D(Collider2D other)
    44.     {
    45.         if (other.CompareTag("Player"))
    46.         {
    47.             playerInRange = false;
    48.             dialogBox.SetActive(false);
    49.         }
    50.     }
    51. }
    52.  
    İ guess İf state broke the code ? But im not sure.
     
  2. Zer0Cool

    Zer0Cool

    Joined:
    Oct 24, 2014
    Posts:
    203
    Make it more simple like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DialogueSystem : MonoBehaviour
    6. {
    7.     public GameObject dialogBox;
    8.     public TMP_Text dialogText;
    9.     public string dialog;
    10.     public bool playerInRange = false;
    11.  
    12.     void Start()
    13.     {
    14.         dialogBox.SetActive(playerInRange);
    15.         dialogText.text = dialog;
    16.     }
    17.  
    18.     private void OnTriggerEnter2D(Collider2D other)
    19.     {
    20.         if (other.CompareTag("Player"))
    21.         {
    22.             playerInRange = true;
    23.             dialogBox.SetActive(playerInRange);
    24.         }
    25.     }
    26.  
    27.     private void OnTriggerExit2D(Collider2D other)
    28.     {
    29.         if (other.CompareTag("Player"))
    30.         {
    31.             playerInRange = false;
    32.             dialogBox.SetActive(playerInRange);
    33.         }
    34.     }
    35. }
    36.  
     
    zeus128 likes this.
  3. zeus128

    zeus128

    Joined:
    Jun 14, 2020
    Posts:
    3
    Thanks for your help and clearing up the code.