Problem With Dialogue Box Triggering

Working on a 2D RPG (pretty simple, first attempt at one).

What is supposed to happen is that when the player is right next to an object, they should be able to press SPACE to bring up flavor text dialogue. Pressing space again makes the text dissapear

The problem is strange and I’m sure it’s just missing some little thing, but basically what happens is that when pressing space next to the object, it only sometimes will pull up the flavor text. Also, sometimes when closing the dialogue box, the dialogue box immediately opens again by itself.

I already tried setting sleep mode on the player object to never sleep, which seemed to help (text appeared more frequently) but didn’t fix the problem.

I used two scripts, one called DialogueController attached to a DialogueManager object, and one called DialogueHolder attached to the object where dialogue can be triggered.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DialogueController : MonoBehaviour {

    public GameObject dBox;
    public Text dText;

    public bool dIsActive;

    // Use this for initialization
    void Start () {
      
    }
  
    // Update is called once per frame
    void Update () {
        if (dIsActive && Input.GetKeyDown(KeyCode.Space)) {
            dBox.SetActive (false);
            dIsActive = false;
        }
      
    }

    public void ShowBox(string dialogue) {
        dIsActive = true;
        dBox.SetActive (true);
        dText.text = dialogue;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueHolder : MonoBehaviour {

    public string dialogue;
    private DialogueController dCon;

    // Use this for initialization
    void Start () {
        dCon = FindObjectOfType<DialogueController> ();
    }
  
    // Update is called once per frame
    void Update () {
      
    }

    void OnTriggerStay2D(Collider2D other) {
        if (other.gameObject.name == "Player1" || other.gameObject.name == "nakee_0") {
            if (Input.GetKeyUp (KeyCode.Space)) {
                dCon.ShowBox (dialogue);
            }
        }
  
    }
}

Would really appreciate some help.

Well, I figured it out with a somewhat different approach. Would still like to understand why what I had wasn’t working.

1 Like

That is a great attitude.

Physics functions like OnTriggerX will only fire during a physics update (FixedUpdate), which can happen less often than regular Update, or potentially multiple times per Update depending on your framerate. That means it can easily miss the frame that you released a key. It’s not a reliable way to poll inputs, and is very likely the reason for the unexpected behavior you saw.

You should always use Update for input detection.

Additionally, you would be better off not using “OnTriggerStay”, but rather using Enter and Exit events to keep a class variable updated like a boolean for whether or not the player is inside it. Then you can check in Update if the key is released, and also check that boolean.

For example, here I keep a reference to the Collider2D currently inside the trigger:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueHolder : MonoBehaviour {

    public string dialogue;
    private DialogueController dCon;
    private Collider2D inside;

    // Use this for initialization
    private void Start() {
        dCon = FindObjectOfType<DialogueController>();
    }

    // Update is called once per frame
    private void Update() {
        if(inside != null && Input.GetKeyUp(KeyCode.Space)) {
            dCon.ShowBox(dialogue);
        }
    }

    private void OnTriggerEnter2D(Collider2D other) {
        if(other.gameObject.name == "Player1" || other.gameObject.name == "nakee_0") {
            inside = other;
        }
    }

    private void OnTriggerExit2D(Collider2D other) {
        if(other == inside) {
            inside = null;
        }
    }
}

Oh, that’s interesting. So not anything wrong with the code per se but rather, a lack of understanding of how Unity works. Definitely good to know in future.

That’s exactly what I did, which indirectly led me to solve the first problem.

Thanks for the information.

1 Like