Loading a diffrent script to a public field at runtime.

Hello!
So i have 2 scripts Dialogue and Npc.
On both of the scripts i have a public string[ ] sentences
I want to have multiple npc-s and when ever i go talk to the npc i want to load the Npc public string senteceses to Dialogue public string sentences.
For npc acctivation i am using raycasting and when ever it detecs the tag npc it starts the Dialogue script.

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

public class Dialog2nd : MonoBehaviour
{
    public TextMeshProUGUI textDisplay;
    public string[] sentences;
    private int index;
    public float typingSpeed;

    public GameObject continueButton;
    public GameObject dialogUI;

    public PlayerController PlayerCtrl;


    void Start()
    {
        dialogUI.SetActive(false);
    }
    void Update()
    {
        if(textDisplay.text == sentences[index])
        {
            continueButton.SetActive(true);
        }

    }
    public IEnumerator Type()
    {
        dialogUI.SetActive(true);
        Debug.Log("Running corutine Type()");
        foreach(char letter in sentences[index].ToCharArray())
        {
            textDisplay.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
    }

    public void NextSetence()
    {
        continueButton.SetActive(false);

        if (index < sentences.Length - 1)
        {
            index++;
            textDisplay.text = "";
            StartCoroutine(Type());
        }else
        {
            textDisplay.text = "";
            continueButton.SetActive(false);
            PlayerCtrl.NpcUnInteract();


        }
    }


}
if (Input.GetKeyDown(Use))
        {
            RaycastHit hit;
            Ray ray = new Ray(transform.position, transform.forward);

            if (Physics.Raycast(ray, out hit, range))
            {
                if (hit.collider.tag == "Npc" && IsInDialog == false)
                {
                    NpcInteratct();
                }

            }


        }
    public void NpcInteratct()
    {
        StartCoroutine(dialog.Type());
        LastMouseSens = mouse.mouseSens;
        IsInDialog = true;
        Time.timeScale = 1f;
        Cursor.lockState = CursorLockMode.Confined;
        mouse.mouseSens = 0f;
        DialogueUi.SetActive(true);
    }

Ok… how do you store your dialogue data and associate a particular set of dialogue to a particular NPC?

On every npc there is a NPC script which stores the data.

Ok simple then. I would change your Type() coroutine to take a parameter like this:

    public IEnumerator Type(string[] dialogue)
    {
        dialogUI.SetActive(true);
        Debug.Log("Running corutine Type()");
        foreach(char letter in dialogue[index].ToCharArray())
        {
            textDisplay.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
    }

Then change your raycast code like this:

if (Input.GetKeyDown(Use))
        {
            RaycastHit hit;
            Ray ray = new Ray(transform.position, transform.forward);
            if (Physics.Raycast(ray, out hit, range))
            {
                if (hit.collider.tag == "Npc" && IsInDialog == false)
                {
                    NPC npc = hit.collider.GetComponent<NPC>();
                    string[] dialogue = npc.DialogueSentences;
                    NpcInteratct(dialogue);
                }
            }
        }
    public void NpcInteratct(string[] dialogue)
    {
        StartCoroutine(dialog.Type(dialogue));
        LastMouseSens = mouse.mouseSens;
        IsInDialog = true;
        Time.timeScale = 1f;
        Cursor.lockState = CursorLockMode.Confined;
        mouse.mouseSens = 0f;
        DialogueUi.SetActive(true);
    }

I made some assumptions about your NPC script:

  • That it is called NPC
  • That the dialogue data on the script is called “DialogueSentences”

Just change my code with whatever the real names of those things are and you should be good to go.

Thank you but there is one more issue
so there is an error on StartCorutine(Type()); in the if statment
When i write StartCorutine(Type(dialogue)); it still gives me an error

public void NextSetence()
    {
        continueButton.SetActive(false);

        if (index < sentences.Length - 1)
        {
            index++;
            textDisplay.text = "";
            StartCoroutine(Type());
        }else
        {
            textDisplay.text = "";
            continueButton.SetActive(false);
            PlayerCtrl.NpcUnInteract();


        }
    }

You are not passing the arguments to Type() that it expects.