can't check if my NPC has a specific script attached
I am making a game and trying to add dialogue options to some of the NPCs. I have it set so that when the first dialogue (which is a pre-made string array) is done, I want it to check and see if the Choices script is attached to the NPC. and if it the NPC has the Choices script attached to it, it activates a panel with buttons that play the next bit of dialogue. If the NPC doesn't have the script, it closes the dialogue box at the end of the first bit of dialogue. It's probably a bit clunky, I know, but it's my first real game. Here's the problem. I can't figure out how to look for an attached script in the if statement. I've been looking through and trying everything I can find, but it always comes back as false, even when the NPC has the Choices script. No solution I can find seems to do the trick. I don't know a lot about coding, but this feels like one of those things that probably has a simple solution. I just can't seem to find it :<
here's the Script. The if statement on line 74 (where it says "if(script != null)" in the NextLine function is the one that keeps coming up false even when the script is there. :
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
public class Dialogue : MonoBehaviour
{
[SerializeField]
public UnityEngine.UI.Text dialogueText;
[SerializeField]
private GameObject dialoguePanel;
[SerializeField]
//public GameObject choicePanel;
private string[] dialogue;
private int dialogueIndex;
private NPC npcScript;
public bool inRange;
//public GameObject ChoiceManager;
private bool neverDone;
public void Awake()
{
//Debug.Log("index referenced? " + ( != null), this);
}
public void Start()
{
neverDone = true;
inRange = false;
}
//void OnCollisionStay(Collision collision)
//{
// if (collision.gameObject.tag == "ChoiceNPC")
// {
// inRange = true;
// Debug.Log("choices availiable");
// }
// else
// {
// Debug.Log("choices not availiable");
// return;
// }
//}
public void StartDialogue(string[] dialogue)
{
dialogueIndex = 0;
this.dialogue = dialogue;
dialoguePanel.SetActive(true);
dialogueText.text = dialogue[dialogueIndex];
}
public void NextLine()
{
Choices script = gameObject.GetComponent<Choices>();
dialogueIndex = Mathf.Min(dialogueIndex + 1, this.dialogue.Length);
if (dialogueIndex >= dialogue.Length)
{
if (neverDone)
{
//if ((gameObject.GetComponents<Choices>().Length != 0))
//if (((gameObject.GetComponent("Choices") as Choices) != null))
//if (FindObjectOfType<NPC>().ChoiceManager == null)
//if (FindObjectOfType<NPC>().ChoiceManager.gameObject.activeSelf)
if(script != null)
{
//Debug.Log("choices are availiable");
ResetDialogue();
neverDone = false;
Debug.Log("Choices are availiable");
gameObject.GetComponent<Choices>().choicePanel.SetActive(true);
//Debug.Log("it was Diallogue line 59");
//ResetDialogue();
//return;
}
else
{
Debug.Log("choices arent availiale");
ResetDialogue();
neverDone = false;
return;
//Debug.Log("choice manager found");
//ResetDialogue();
//neverDone = false;
//FindObjectOfType<NPC>().choicePanel.SetActive(true);
}
}
else
{
Debug.Log("it was Diallogue line 79");
ResetDialogue();
neverDone = true;
return;
}
}
else
{
dialogueText.text = dialogue[dialogueIndex];
}
}
public void PreviousLine()
{
if (dialogueIndex == 0)
{
return;
}
dialogueIndex = Mathf.Max(dialogueIndex - 1, 0);
dialogueText.text = dialogue[dialogueIndex];
}
public void ResetDialogue()
{
dialogue = null;
dialogueText.text = "";
dialoguePanel.SetActive(false);
dialogueIndex = 0;
}
}
At just a quick look, assu$$anonymous$$g the script is named Choices, your first commented IF statement SHOULD work, as far as I can see. It would get a list of all Choices scripts attached to the gameobject, and if the list length >0 (has at least one) then it would return true. Assu$$anonymous$$g that this dialog script is attached to the same gameobject as the choices script.
What are you getting when this if statement is used?
Just to help, try adding Debug.Log("Number of Choices found: " + gameObject.GetComponents<Choices>().Length);
right after the if(NeverDone) so you can see what this is returning.