Unity Dialogue System only active when NPC is in range?

Hi, I’ve followed a tutorial and created a script for an NPC Dialogue. Everything works. But the dialogue always starts when I start the game, even when no NPC is in range. How can I fix my script, so the dialogue only begins when I interact/collide with an NPC?

Hi, I’ve followed a tutorial and created a script for an NPC Dialogue. Everything works. But the dialogue always starts when I start the game, even when no NPC is in range. How can I fix my script, so the dialogue only begins when I interact/collide with an NPC?

using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class NPCInteraction : MonoBehaviour
{
public TextMeshProUGUI textDisplay;
public string sentences;
private int index;
public float typingSpeed;
public GameObject contButton;

void Update()
{
    if (textDisplay.text == sentences[index])
    {
        contButton.SetActive(true);
    }
}

void Start()
{
    StartCoroutine(Type());

}

IEnumerator Type()
{
    foreach (char letter in sentences[index].ToCharArray())
    {
        textDisplay.text += letter;
        yield return new WaitForSeconds(typingSpeed);
    }
}

public void nextSentence()
{
    contButton.SetActive(false);

    if (index < sentences.Length)
    {
        index++;
        textDisplay.text = "";
        StartCoroutine(Type());
    }
    else
    {
        textDisplay.text = "";
        contButton.SetActive(false);
    }

}

}

Don’t start it in Start if you don’t want it to start immediately, if you want it to start when you collide with an NPC then use OnCollision to start your coroutine, if you want it to start when the NPC is in range you need to define the range and check in update if an NPC is in range with Vector3.Distance if you want to it to start when you click on a NPC or press a button you have to write the logic for that. Just basic code flow.