Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Loading a diffrent script to a public field at runtime.

Discussion in 'Scripting' started by muumif, Jan 20, 2021.

  1. muumif

    muumif

    Joined:
    Aug 16, 2017
    Posts:
    3
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class Dialog2nd : MonoBehaviour
    7. {
    8.     public TextMeshProUGUI textDisplay;
    9.     public string[] sentences;
    10.     private int index;
    11.     public float typingSpeed;
    12.  
    13.     public GameObject continueButton;
    14.     public GameObject dialogUI;
    15.  
    16.     public PlayerController PlayerCtrl;
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         dialogUI.SetActive(false);
    22.     }
    23.     void Update()
    24.     {
    25.         if(textDisplay.text == sentences[index])
    26.         {
    27.             continueButton.SetActive(true);
    28.         }
    29.  
    30.     }
    31.     public IEnumerator Type()
    32.     {
    33.         dialogUI.SetActive(true);
    34.         Debug.Log("Running corutine Type()");
    35.         foreach(char letter in sentences[index].ToCharArray())
    36.         {
    37.             textDisplay.text += letter;
    38.             yield return new WaitForSeconds(typingSpeed);
    39.         }
    40.     }
    41.  
    42.     public void NextSetence()
    43.     {
    44.         continueButton.SetActive(false);
    45.  
    46.         if (index < sentences.Length - 1)
    47.         {
    48.             index++;
    49.             textDisplay.text = "";
    50.             StartCoroutine(Type());
    51.         }else
    52.         {
    53.             textDisplay.text = "";
    54.             continueButton.SetActive(false);
    55.             PlayerCtrl.NpcUnInteract();
    56.  
    57.  
    58.         }
    59.     }
    60.  
    61.  
    62. }
    63.  
    Code (CSharp):
    1.        
    2. if (Input.GetKeyDown(Use))
    3.         {
    4.             RaycastHit hit;
    5.             Ray ray = new Ray(transform.position, transform.forward);
    6.  
    7.             if (Physics.Raycast(ray, out hit, range))
    8.             {
    9.                 if (hit.collider.tag == "Npc" && IsInDialog == false)
    10.                 {
    11.                     NpcInteratct();
    12.                 }
    13.  
    14.             }
    15.  
    16.  
    17.         }
    18.     public void NpcInteratct()
    19.     {
    20.         StartCoroutine(dialog.Type());
    21.         LastMouseSens = mouse.mouseSens;
    22.         IsInDialog = true;
    23.         Time.timeScale = 1f;
    24.         Cursor.lockState = CursorLockMode.Confined;
    25.         mouse.mouseSens = 0f;
    26.         DialogueUi.SetActive(true);
    27.     }
    28.  
    29.  
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Ok... how do you store your dialogue data and associate a particular set of dialogue to a particular NPC?
     
  3. muumif

    muumif

    Joined:
    Aug 16, 2017
    Posts:
    3
    On every npc there is a NPC script which stores the data.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Ok simple then. I would change your Type() coroutine to take a parameter like this:
    Code (CSharp):
    1.     public IEnumerator Type(string[] dialogue)
    2.     {
    3.         dialogUI.SetActive(true);
    4.         Debug.Log("Running corutine Type()");
    5.         foreach(char letter in dialogue[index].ToCharArray())
    6.         {
    7.             textDisplay.text += letter;
    8.             yield return new WaitForSeconds(typingSpeed);
    9.         }
    10.     }
    Then change your raycast code like this:
    Code (CSharp):
    1. if (Input.GetKeyDown(Use))
    2.         {
    3.             RaycastHit hit;
    4.             Ray ray = new Ray(transform.position, transform.forward);
    5.             if (Physics.Raycast(ray, out hit, range))
    6.             {
    7.                 if (hit.collider.tag == "Npc" && IsInDialog == false)
    8.                 {
    9.                     NPC npc = hit.collider.GetComponent<NPC>();
    10.                     string[] dialogue = npc.DialogueSentences;
    11.                     NpcInteratct(dialogue);
    12.                 }
    13.             }
    14.         }
    15.     public void NpcInteratct(string[] dialogue)
    16.     {
    17.         StartCoroutine(dialog.Type(dialogue));
    18.         LastMouseSens = mouse.mouseSens;
    19.         IsInDialog = true;
    20.         Time.timeScale = 1f;
    21.         Cursor.lockState = CursorLockMode.Confined;
    22.         mouse.mouseSens = 0f;
    23.         DialogueUi.SetActive(true);
    24.     }
    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.
     
  5. muumif

    muumif

    Joined:
    Aug 16, 2017
    Posts:
    3
    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
    Code (CSharp):
    1.  
    2. public void NextSetence()
    3.     {
    4.         continueButton.SetActive(false);
    5.  
    6.         if (index < sentences.Length - 1)
    7.         {
    8.             index++;
    9.             textDisplay.text = "";
    10.             StartCoroutine(Type());
    11.         }else
    12.         {
    13.             textDisplay.text = "";
    14.             continueButton.SetActive(false);
    15.             PlayerCtrl.NpcUnInteract();
    16.  
    17.  
    18.         }
    19.     }
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    You are not passing the arguments to
    Type()
    that it expects.