Help with interactable dialogue script

I’m going to start off by clarifying that yes, I know I’m in over my head trying to grapple things that I know nothing about. But this is how I do things. I try and make things work, and if I can’t find a solution on my own, I seek help.
SO! I’m trying to put together an intractable script, where a player can respond to an NPC in a dialogue window. I’m trying to make this scripting setup flexible enough to where it can trigger events such as spawning objects. What I have is based on this youtube tutorial by gamesplusjames.
The scripts I have are:
Dialogue1

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

public class Dialogue1 : MonoBehaviour {
    public string[] NPCText;
    public string[] PCReply;
    public bool ConvoEnd = false;
    public string CurrentConv;

    public void Test()
    {
        while(ConvoEnd == false)
        NPCText[1] = "Hi!";
        NPCText[2] = "What would you like to do?";
        PCReply[1] = "1) Nothing, just wandering.";
        PCReply[2] = "2) I'm not sure. Do you have any suggestions?";
        var input = Input.inputString;
        switch (input)
        {
            case "1":
                NPCText[3] = "Alright, take care.";
                ConvoEnd = true;
                break;
            case "2":
                CurrentConv = "TestA";
                TestA();
                ConvoEnd = true;
                break;
            default:
                break;
        }
    }
    public void TestA()
    {
        while (ConvoEnd == false)
        NPCText[1] = "Hmm. I don't know...\n This isn't getting anyone anywyere.";
        PCReply[1] = "1) Yeah, I suppose I'll leave.";
        PCReply[2] = "2) What are you talking about? This is fun!";
        PCReply[3] = "3) Let's go back a step...";
        var input = Input.inputString;
        switch (input)
        {
            case "1":
                NPCText[4] = "Alright, take care.";
                ConvoEnd = true;
                break;
            case "2":
                NPCText[4] = "No, it's boring. Go away.";
                ConvoEnd = true;
                break;
            case "3":
                NPCText[4] = "Okay.";
                CurrentConv = "Test";
                Test();
                ConvoEnd = true;
                break;
            default:
                break;
        }
    }
}

Speaker

using UnityEngine;
using System.Collections;

public class Speaker : MonoBehaviour {
    public int startLine;

    public int endLine;

    public TextManager theTextBox;

    public bool requireButton;

    private bool waitForPress;

    public bool destroyWhenActivate;

    public string StartDialogue;

    // Use this for initialization
    void Start()
    {
        theTextBox = FindObjectOfType<TextManager>();
    }

    // Update is called once per frame
    void Update()
    {
        if (waitForPress && Input.GetKeyDown(KeyCode.J))
        {
            //theTextBox.ReloadScript(StartDialogue);
            theTextBox.currentLine = startLine;
            theTextBox.endAtLine = endLine;
            theTextBox.EnableTextBox();
            if (destroyWhenActivate)
            {
                Destroy(gameObject);
            }
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (requireButton)
            {
                waitForPress = true;
                return;
            }
            theTextBox.ReloadScript(StartDialogue);
            theTextBox.currentLine = startLine;
            theTextBox.endAtLine = endLine;
            theTextBox.EnableTextBox();
            if (destroyWhenActivate)
            {
                Destroy(gameObject);
            }
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            waitForPress = false;
        }
    }
}

TextManager

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

public class TextManager : MonoBehaviour {
    public GameObject textBox;
    public Speaker whoSpeaks;
    public Dialogue1 theTextFile;
    public int currentLine;
    public int endAtLine;
    public Player player;
    public bool isActive;
    public bool stopPlayerMovement;
    private bool isTyping = false;
    private bool cancelTyping = false;
    public float typeSpeed;
    public Text theText;
    public string[] NPCLines;


    // Use this for initialization
    void Start () {
     
 
        if (endAtLine == 0)
        {
            endAtLine = NPCLines.Length - 1;
        }
        if (isActive)
        {
            EnableTextBox();
        }
        else {
            DisableTextBox();
        }
    }
 
    // Update is called once per frame
    void Update () {
        NPCLines = theTextFile.NPCText;
        if (!isActive)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (!isTyping)
            {
                currentLine += 1;
                if (currentLine > endAtLine)
                {
                    DisableTextBox();
                }
                else {
                    StartCoroutine(TextScroll(NPCLines[currentLine]));

                }

            }
            else if (isTyping && !cancelTyping)
            {
                cancelTyping = true;
            }
        }
    }
    private IEnumerator TextScroll(string lineOfText)
    {
        int letter = 0;
        theText.text = "";
        isTyping = true;
        cancelTyping = false;
        while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
        {
            theText.text += lineOfText[letter];
            letter += 1;
            yield return new WaitForSeconds(typeSpeed);
        }
        theText.text = lineOfText;
        isTyping = false;
        cancelTyping = false;
    }
    public void EnableTextBox()
    {
        textBox.SetActive(true);
        isActive = true;
        if (stopPlayerMovement)
        {
            player.canMove = false;
        }
        StartCoroutine(TextScroll(NPCLines[currentLine]));
    }
    public void DisableTextBox()
    {
        textBox.SetActive(false);
        isActive = false;
        player.canMove = true;
    }

    public void ReloadScript(TextAsset Dialogue)
    {
        if (Dialogue != null)
        {
            NPCLines = new string[1];
            //NPCLines = (Dialogue.text.Split('\n'));
        }
    }

}

The issue I’m running in to is, as far as I’m aware, getting the TextManager script to read the method from Dialogue as indicated from Speaker (StartDialogue is the name of the method in Dialogue)

Thanks in advance for any help provided!

Vedrit, couple of things

  1. if you could, for everyones convenience, give the name of the files instead of what they’re for, your telling us what they’re for in the description of your problem

  2. normally [c-o-d-e] sample code here [/code] is preffed and if your concerned with length there is the spoiler tag

  3. problem not clear, what is happening? I looked at your TextManager, and your reference to the Speaker script is never used…so…yea, thats an issue.

  4. Dialogue has no reference to TextManager, I would have made a function that sends the string data to a function in Dialogue.

best of luck!

I adjusted the first post to fit your first two points.

I did some fiddling and lines 40-42 of TextManager are adjusted to this

whereToStart = whoSpeaks.StartDialogue;
        NPCLines = theTextFile.NPCText;
        theTextFile.Invoke(whereToStart, 0f);

Probably not the best place for them… No errors in VS
I’m getting an issue with the ReloadScript function in TextManager. I can’t seem to figure out how to convert the strings I have available to text for the UI.

Although, after some fiddling, I think I may have sorted it out… I’m going to test it out in Unity and see how it goes.

Edit: I’m not getting any errors, and the text window pops up, but it doesn’t show any text other than what is in the text box by default when created. Other than that, I found out I had foolishly removed a line that found the player, which is kinda important.

Updated scripts

Dialogue1

using UnityEngine;
using System.Collections;

public class Dialogue1 : MonoBehaviour {
    public string[] NPCText;
    public string[] PCReply;
    public int NPCEndAt;
    public int PlayerEndAt;
    public bool ConvoEnd = false;
    public string CurrentConv;

    public void Test()
    {
        while(ConvoEnd == false)
        NPCText[1] = "Hi!";
        NPCText[2] = "What would you like to do?";
        PCReply[1] = "1) Nothing, just wandering.";
        PCReply[2] = "2) I'm not sure. Do you have any suggestions?";
        NPCEndAt = NPCText.Length  - 1;
        PlayerEndAt = PCReply.Length - 1;
        var input = Input.inputString;
        switch (input)
        {
            case "1":
                NPCText[3] = "Alright, take care.";
                ConvoEnd = true;
                break;
            case "2":
                CurrentConv = "TestA";
                TestA();
                ConvoEnd = true;
                break;
            default:
                break;
        }
    }
    public void TestA()
    {
        while (ConvoEnd == false)
        NPCText[1] = "Hmm. I don't know...\n This isn't getting anyone anywyere.";
        PCReply[1] = "1) Yeah, I suppose I'll leave.";
        PCReply[2] = "2) What are you talking about? This is fun!";
        PCReply[3] = "3) Let's go back a step...";
        NPCEndAt = NPCText.Length - 1;
        PlayerEndAt = PCReply.Length - 1;
        var input = Input.inputString;
        switch (input)
        {
            case "1":
                NPCText[4] = "Alright, take care.";
                ConvoEnd = true;
                break;
            case "2":
                NPCText[4] = "No, it's boring. Go away.";
                ConvoEnd = true;
                break;
            case "3":
                NPCText[4] = "Okay.";
                CurrentConv = "Test";
                Test();
                ConvoEnd = true;
                break;
            default:
                break;
        }
    }
}

Speaker

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

public class Speaker : MonoBehaviour {
    public int startLine;

    public int endLine;

    public TextManager theTextBox;

    public bool requireButton;

    private bool waitForPress;

    public bool destroyWhenActivate;

    public string StartDialogue;

    // Use this for initialization
    void Start()
    {
        theTextBox = FindObjectOfType<TextManager>();
    }

    // Update is called once per frame
    void Update()
    {
        if (waitForPress && Input.GetKeyDown(KeyCode.J))
        {
            //theTextBox.ReloadScript(StartDialogue);
            theTextBox.currentLine = startLine;
            //theTextBox.endAtLine = endLine;
            theTextBox.EnableTextBox();
            if (destroyWhenActivate)
            {
                Destroy(gameObject);
            }
        }
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (requireButton)
            {
                waitForPress = true;
                return;
            }
            theTextBox.ReloadScript(StartDialogue);
            theTextBox.currentLine = startLine;
            //theTextBox.endAtLine = endLine;
            theTextBox.EnableTextBox();
            if (destroyWhenActivate)
            {
                Destroy(gameObject);
            }
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            waitForPress = false;
        }
    }
}

TextManager

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

public class TextManager : MonoBehaviour {
    public GameObject textBox;
    public Speaker whoSpeaks;
    public Dialogue1 theTextFile;
    public int currentLine;
    public int endAtLine;
    public Player player;
    public bool isActive;
    public bool stopPlayerMovement;
    private bool isTyping = false;
    private bool cancelTyping = false;
    public float typeSpeed;
    public Text theText;
    public string[] NPCLines;
    public string whereToStart;

    // Use this for initialization
    void Start () {

        player = FindObjectOfType<Player>();

        if (endAtLine == 0)
        {
            endAtLine = NPCLines.Length - 1;
        }
        if (isActive)
        {
            EnableTextBox();
        }
        else {
            DisableTextBox();
        }
    }
  
    // Update is called once per frame
    void Update () {
        if (!isActive)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (!isTyping)
            {
                currentLine += 1;
                if (currentLine > endAtLine)
                {
                    DisableTextBox();
                }
                else {
                    StartCoroutine(TextScroll(NPCLines[currentLine]));
                }
            }
            else if (isTyping && !cancelTyping)
            {
                cancelTyping = true;
            }
        }
    }
    private IEnumerator TextScroll(string lineOfText)
    {
        int letter = 0;
        theText.text = "";
        isTyping = true;
        cancelTyping = false;
        while (isTyping && !cancelTyping && (letter < lineOfText.Length - 1))
        {
            theText.text += lineOfText[letter];
            letter += 1;
            yield return new WaitForSeconds(typeSpeed);
        }
        theText.text = lineOfText;
        isTyping = false;
        cancelTyping = false;
    }
    public void EnableTextBox()
    {
        textBox.SetActive(true);
        isActive = true;
        if (stopPlayerMovement)
        {
            player.canMove = false;
        }
        StartCoroutine(TextScroll(NPCLines[currentLine]));
    }
    public void DisableTextBox()
    {
        textBox.SetActive(false);
        isActive = false;
        if (player.canMove == false)
        {
            player.canMove = true;
        }
    }

    public void ReloadScript(string theText)
    {
        whereToStart = whoSpeaks.StartDialogue;
        theTextFile.Invoke(whereToStart, 0f);
        NPCLines = theTextFile.NPCText;
        endAtLine = theTextFile.NPCEndAt;
        if (theText != null)
        {
            NPCLines = new string[1];
            //NPCLines = (Dialogue.text.Split('\n'));
        }
    }
}

Edit: I think part of the issue is that TextManager isn’t being told what objects/files it’s supposed to be getting this information from, and in my current state (very hungry and very tired) I can’t figure out how to have this dynamically occur.

Sorry for multiple posts in a row, but I think I’m making progress. TextManager will now pull functions from Dialogue1, but it keeps throwing array out of range.
TextManager

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

public class TextManager : MonoBehaviour {
    public GameObject textBox;
    public GameObject speaking;
    public Speaker whoSpeaks;
    public Dialogue1 theTextFile;
    public int currentLine = 0;
    public int endAtLine;
    public Player player;
    public bool isActive;
    public bool stopPlayerMovement;
    private bool isTyping = false;
    private bool cancelTyping = false;
    public float typeSpeed;
    public string theText;
    public string[] NPCLines;
    public string whereToStart;


    // Use this for initialization
    void Start () {

        player = FindObjectOfType<Player>();

        if (endAtLine == 0)
        {
            endAtLine = NPCLines.Length - 1;
        }
        if (isActive)
        {
            EnableTextBox();
        }
        else {
            DisableTextBox();
        }
    }
  
    // Update is called once per frame
    void Update () {
        if (!isActive)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.Return))
        {
            if (!isTyping)
            {
                currentLine += 1;
                if (currentLine > endAtLine)
                {
                    DisableTextBox();
                }
                else {
                    StartCoroutine(TextScroll(NPCLines[currentLine]));

                }

            }
            else if (isTyping && !cancelTyping)
            {
                cancelTyping = true;
            }
        }
    }
    private IEnumerator TextScroll(string NPCLines)
    {
        int letter = 0;
        theText = "";
        isTyping = true;
        cancelTyping = false;
        while (isTyping && !cancelTyping && (letter < NPCLines.Length - 1))
        {
            theText += NPCLines[letter];
            letter += 1;
            yield return new WaitForSeconds(typeSpeed);
        }
        theText = NPCLines;
        isTyping = false;
        cancelTyping = false;
    }
    public void EnableTextBox()
    {
        textBox.SetActive(true);
        isActive = true;
        if (stopPlayerMovement)
        {
            player.canMove = false;
        }
        StartCoroutine(TextScroll(NPCLines[0]));
    }
    public void DisableTextBox()
    {
        textBox.SetActive(false);
        isActive = false;
        if (player.canMove == false)
        {
            player.canMove = true;
        }
    }

    public void ReloadScript()
    {
        //whereToStart = "Test";
        whoSpeaks = speaking.GetComponent<Speaker>();
        whereToStart = whoSpeaks.StartDialogue;
        theTextFile = speaking.GetComponent<Dialogue1>();
        theTextFile.Invoke(whereToStart, 0f);
        //NPCLines = theTextFile.NPCText;
        endAtLine = theTextFile.NPCEndAt;
        if (theText != null)
        {
            NPCLines = new string[1];
            NPCLines = theTextFile.NPCText;
            //NPCLines = (Dialogue.text.Split('\n'));
        }
    }

}

I pretty much rebuilt the code and it is working now, so…Thanks for your time, RavenMikal and anyone else who even looked at this thread, heh.

Moderators, this thread can be closed now.

sweet =)