Dialogue script using box collider
I am trying to have dialogue triggered by the player entering a box collider. When I attach my script the game freezes in playing and my text moves across the screen.
using UnityEngine; using System.Collections; using UnityEngine.UI; using System.Collections.Generic; [RequireComponent(typeof(BoxCollider))]
public class Dialogue : MonoBehaviour {
public List<string> dialogueList = new List<string>();
public string convoOverText = "HAHAHAHHAHAHHAHAHahahahahaaaa";
int currentDialogueLine = 0;
bool alreadyTriggerEntered = false;
Text dialogueText;
// Use this for initialization
void Start () {
dialogueText = GameObject.Find("DialogueTextBox").GetComponent<Text>();
}
void Converse ()
{
if (dialogueList.Count > currentDialogueLine)
{
dialogueText.text = dialogueList[currentDialogueLine];
Debug.Log(dialogueList[currentDialogueLine]);
currentDialogueLine++;
}
else if (dialogueList.Count <= currentDialogueLine)
{
dialogueText.text = convoOverText;
Debug.Log(convoOverText);
}
}
void OnTriggerEnter (Collider other)
{
if (!alreadyTriggerEntered)
{
alreadyTriggerEntered = true;
Converse();
}
}
void OnTriggerStay (Collider other)
{
if (other.tag == "Player")
{
if (Input.GetMouseButtonDown(0))
{
Converse();
}
}
}
}`
Answer by Adamcbrz · Sep 05, 2016 at 01:37 PM
I am having a hard time making out the actual question here. But I think you are trying to say that the Dialogue triggers at the beginning of the scene instead of when the player triggers it.
If that assumption is correct then I would suggest either: 1) check to see if the Collider that is trigging OnTriggerEnter is the Player. 2) Adjust your Layers and Physics so that only the player can collide with the dialogue object. This does the same thing as 1 but is more efficient because the physics system doesn't check collision on anything except the player. Physics Collision Settings can be found under Edit->Project Settings->Physics.