So I have an issue, I have a Dialogue system and a player raycast interaction. The raycast is not caliing the MultipleResponseDialogueSystem.
The Player interaction code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Player : MonoBehaviour
{
public float interactionDistance = 5f;
public KeyCode interactKey = KeyCode.E;
private bool inDialogue = false;
private MultipleResponseDialogueSystem npcDialogueSystem;
void Update()
{
if (Input.GetKeyDown(interactKey))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, interactionDistance))
{
if (hit.collider.CompareTag("NPC"))
{
Debug.Log("Interacting with object " + npcDialogueSystem);
Debug.DrawRay(transform.position, transform.forward * interactionDistance, Color.green, 1f);
npcDialogueSystem = hit.collider.GetComponent<MultipleResponseDialogueSystem>();
inDialogue = true;
npcDialogueSystem.ShowDialogueOptions();
}
}
}
}
}
And this is my Dialogue code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MultipleResponseDialogueSystem : MonoBehaviour
{
public string[] dialogueOptions;
public string[] npcResponses;
private bool inDialogue = false;
public void StartDialogue()
{
// Check if the player is already in dialogue
if (inDialogue)
{
return;
}
// Start the dialogue by showing the dialogue options
ShowDialogueOptions();
// Set inDialogue to true
inDialogue = true;
}
public void ShowDialogueOptions()
{
// Display the dialogue options to the player using Unity's UI system
// This can be done by creating a new Canvas and adding UI elements like buttons and text
// Create a new Canvas
GameObject canvasGameObject = new GameObject("Dialogue Canvas");
Canvas canvas = canvasGameObject.AddComponent<Canvas>();
canvas.renderMode = RenderMode.ScreenSpaceOverlay;
// Create a new Panel
GameObject panelGameObject = new GameObject("Dialogue Panel");
panelGameObject.transform.SetParent(canvas.transform);
RectTransform panelRectTransform = panelGameObject.AddComponent<RectTransform>();
panelRectTransform.anchorMin = new Vector2(0.5f, 0f);
panelRectTransform.anchorMax = new Vector2(0.5f, 0f);
panelRectTransform.pivot = new Vector2(0.5f, 0f);
panelRectTransform.anchoredPosition = new Vector2(0f, 50f);
panelRectTransform.sizeDelta = new Vector2(400f, 200f);
Image panelImage = panelGameObject.AddComponent<Image>();
panelImage.color = new Color(0f, 0f, 0f, 0.8f);
// Create buttons for each dialogue option
for (int i = 0; i < dialogueOptions.Length; i++)
{
GameObject buttonGameObject = new GameObject("Dialogue Option " + (i + 1));
buttonGameObject.transform.SetParent(panelGameObject.transform);
RectTransform buttonRectTransform = buttonGameObject.AddComponent<RectTransform>();
buttonRectTransform.anchorMin = new Vector2(0f, 1f - ((i + 1) * 0.25f));
buttonRectTransform.anchorMax = new Vector2(1f, 1f - (i * 0.25f));
buttonRectTransform.pivot = new Vector2(0.5f, 0.5f);
buttonRectTransform.anchoredPosition = Vector2.zero;
buttonRectTransform.sizeDelta = Vector2.zero;
Button button = buttonGameObject.AddComponent<Button>();
Text buttonText = buttonGameObject.AddComponent<Text>();
buttonText.text = dialogueOptions[i];
buttonText.color = Color.white;
buttonText.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
buttonText.fontSize = 24;
buttonText.alignment = TextAnchor.MiddleCenter;
// Add an onClick event to the button that calls RespondToDialogueOption on the NPC GameObject
int index = i;
button.onClick.AddListener(() => { gameObject.GetComponent<MultipleResponseDialogueSystem>().RespondToDialogueOption(index); });
}
}
public void RespondToDialogueOption(int index)
{
// Display the NPC's response to the selected dialogue option
Debug.Log(npcResponses[index]);
// End the dialogue by destroying the Dialogue Canvas and setting inDialogue to false
inDialogue = false;
Destroy(GameObject.Find("Dialogue Canvas"));
}
}