- Home /
How to detect which how many of a type a variable a script has
I am trying to make NPC's that have a text box that displays a random canned message from a script. My only problem is that every NPC has this same script and each NPC has a different number of canned messages. So I need to write a script that can adapt to the number of canned messages to say that have been added to that individual script in the editor and pick random between those messages. Here is my script:
public class BasicNPCPrototype : MonoBehaviour
{
private bool knowBefore;
private int whichDialog;
private DialogueManager dialogueManager;
public int numberOfDialogues;
public Dialogue firstMeet;
public Dialogue dialogue1;
public Dialogue dialogue2;
// Use this for initialization
void Start ()
{
dialogueManager = FindObjectOfType<DialogueManager>();
}
// Update is called once per frame
void Update ()
{
}
public void Speak()
{
// Choose what to say
whichDialog = Random.Range(1, numberOfDialogues);
// If this NPC has never been talked to before
if(!knowBefore)
{
dialogueManager.StartDialogue(firstMeet);
knowBefore = true;
}
else
{
// Call the "StartDialogue" function in the DialogueManager and pass in our dialogue variable
dialogueManager.StartDialogue(dialogue1);
}
}
}
I need some way of being able to change the number of Dialogue variables in the editor and have the computer look at the number, put it into an int, and use it in Random.Range. But my biggest challenge will be creating the Dialogue variables in the editor and getting the computer to see it. Thanks.
Answer by Bruno2907 · Mar 29, 2019 at 01:38 AM
Hi, if I understood your problem correctly, what you need to do is to use a dialog array and then get the number of dialog using [array-name].Length Here, try this:
public class BasicNPCPrototype : MonoBehaviour
{
private bool knowBefore;
private DialogueManager dialogueManager;
public Dialogue firstMeet;
public Dialogue[] dialogue;
// Use this for initialization
void Start ()
{
dialogueManager = FindObjectOfType<DialogueManager>();
}
// Update is called once per frame
void Update ()
{
}
public void Speak()
{
// If this NPC has never been talked to before
if(!knowBefore)
{
dialogueManager.StartDialogue(firstMeet);
knowBefore = true;
}
else
{
// Call the "StartDialogue" function in the DialogueManager and pass in our dialogue variable
dialogueManager.StartDialogue(Random.Range(1, dialogue.Length));
}
}
}
@Bruno2907 Hi, sorry for the late reply. It says it can't convert a Dialogue to int because the Dialogue$$anonymous$$anager is expecting a Dialogue variable that looks like this:
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3, 10)]
public string[] sentences;
}
So while you're on the right track I need a way of deciding which dialogue to send to the Dialogue$$anonymous$$anager and then getting that and sending it. You have provided me with a way to pick a random dialogue, can you show me how I would get the dialogue out of the other dialogues in the array to send it to the Dialogue$$anonymous$$anager?