- Home /
[SOLVED] Accessing Specific Instance of Variable on Script Across Multiple Objects
I'm trying to build a basic dialogue interface (and am struggling as I cannot find any good tutorials on dialogue systems, hint hint ;) ).
using UnityEngine;
using System.Collections;
public interface IDialogue{
string prompt;
string[] responses;
void Speak();
void Respond();
}
This interface is implemented by my NPCDialogue script which is attached to multiple NPCs.
using UnityEngine;
using System.Collections;
public class NPCDialogue : MonoBehaviour, IDialogue {
private Transform player; // Reference to the player's transform.
private HashIDs hash;
private GUIText dialogue;
private GameObject response_menu;
private bool inRadius = false;
public string NPC_Name;
void Awake(){
player = GameObject.FindGameObjectWithTag(Tags.player).transform;
dialogue = GameObject.FindWithTag (Tags.dialogue).guiText;
dialogue.text = null;
NPC_Name = transform.parent.name;
print ("NPC name: " + NPC_Name); //Returns correctly for each NPC
//fileManager.Instance.parseXMLFile ("Data/XMLScripts", NPC_Name, "xml", "plaintext");
}
void OnTriggerEnter(Collider col){
inRadius = true;
Converse (); //Returns correct, NPC specific variable
}
void OnTriggerExit(Collider col){
dialogue.text = null;
}
public void Speak(){
dialogue.text = "My name is " + this.gameObject.GetComponent<NPCDialogue>().NPC_Name;
print ("Speaking with: " + NPC_Name);
}
public void Respond() {
}
void Converse(){
Speak ();
}
void Update(){
if (inRadius) {
if (Input.GetButtonDown ("Interact")) {
//Converse (); //Returns most recent variables
}
}
}
}
I need to be able to interact with each NPC using their unique versions of these variables. The problem is that the only way I can get this to work is to call Converse() from OnTriggerEnter() as when I call it from Input in Update() it always outputs the most recently initialised version of the NPC_Name variable, and not the instance specific to the NPC that I am talking to.
I understand why this is the case, but cannot seem to find any way around this after Googling and searching the forums for the past two hours. Any help would be greatly appreciated but code examples would be excellent!
EDIT: Partially Solved This doesn't really answer my question but I have found a workaround, by checking for GetButtonDown in OnTriggerStay. (I am unable to close this question myself)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dialogue character emotions system 1 Answer
Change colour of background in editor's object previews? 0 Answers
How to force refresh mouse position? 2 Answers