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.