Import camera information from one script to another
Hey guys, so, I'm pretty new to c# and followed some tutorials to set up two scripts that run the dialogue system. The first script is universal and brings up the visuals assets of dialogue. The second script is attached to each individual instance/npc and contains unique dialogue that is then imported to the main script to be brought up on screen. I have cameras set up for each NPC, so that when you talk to them it switches to a unique view (think N64 Zelda, kinda). I can't seem to figure out how to get the camera details to import into the main script, though.
The codes posted don't have an attempt to import the data in them.
Here is the universal code for bringing up the visual assets: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityStandardAssets.Characters.FirstPerson;
public class TextBoxManager : MonoBehaviour {
public Camera playercam;
public Camera dialogueCam;
public GameObject textBox;
public Text theText;
public TextAsset textFile;
public string[] textLines;
public int currentTextLine;
public int endTextLine;
public FirstPersonController player;
public ActivateDialogue activateDialogue;
public bool isActive;
// Use this for initialization
void Start()
{
player = FindObjectOfType<FirstPersonController>();
if (textFile != null)
{
textLines = (textFile.text.Split('\n'));
}
if(endTextLine == 0)
{
endTextLine = textLines.Length - 1;
}
if (isActive)
{
EnableTextBox();
} else
{
DisableTextBox();
}
}
void Update()
{
if (!isActive)
{
player.GetComponent<FirstPersonController>().enabled = true;
return;
} else
{
player.GetComponent<FirstPersonController>().enabled = false;
}
theText.text = textLines[currentTextLine];
if (Input.GetKeyDown(KeyCode.Space))
{
currentTextLine += 1;
}
if(currentTextLine > endTextLine)
{
DisableTextBox();
}
}
public void EnableTextBox()
{
textBox.SetActive(true);
isActive = true;
playercam.enabled = false;
dialogueCam.enabled = true;
}
public void DisableTextBox()
{
textBox.SetActive(false);
isActive = false;
playercam.enabled = true;
dialogueCam.enabled = false;
}
public void ReloadScript(TextAsset theText)
{
if(theText != null)
{
textLines = new string[1];
textLines = (theText.text.Split('\n'));
}
}
}
And here is the script that the unique information is attached to, that is then imported into that first script: using UnityEngine; using System.Collections;
public class ActivateDialogue : MonoBehaviour {
public Camera dialogueCam;
public TextAsset theText;
public int startLine;
public int endLine;
public TextBoxManager theTextManager;
public bool destroyWhenActivated;
public bool requireButtonPress;
private bool waitForPress;
private bool stay;
// Use this for initialization
void Start () {
theTextManager = FindObjectOfType<TextBoxManager>();
}
// Update is called once per frame
void Update () {
if(waitForPress && Input.GetKeyDown(KeyCode.E))
{
theTextManager.ReloadScript(theText);
theTextManager.currentTextLine = startLine;
theTextManager.endTextLine = endLine;
theTextManager.EnableTextBox();
if (destroyWhenActivated)
{
Destroy(gameObject);
}
}
//Update if player leaves collider
if (stay)
{
stay = false;
}
else waitForPress = false;
}
//What happens when player is in collider
void OnTriggerStay(Collider other)
{
if (other.name == "Player")
{
stay = true;
if (requireButtonPress)
{
waitForPress = true;
return;
}
theTextManager.ReloadScript(theText);
theTextManager.currentTextLine = startLine;
theTextManager.endTextLine = endLine;
theTextManager.EnableTextBox();
if (destroyWhenActivated)
{
Destroy(gameObject);
}
}
}
}
So the second script goes on each NPC and I apply their unique camera to "dialogueCam", but I can't seem to figure out how to get that information into the first scripts "dialogueCam" bracket...
Thanks for any help you may have.
Answer by ElijahShadbolt · Nov 15, 2016 at 06:01 AM
In the OnTriggerStay()
function of the ActivateDialogue script, just before calling theTextManager.EnableTextBox()
, you could add this line:
theTextBox.dialogueCam = dialogueCam;
This changes the TextBoxManager's dialogueCam
to reference that specific ActivateDialogue script's dialogueCam
.
So simple! Thank you! It seems the more trouble I have the more complex I try to make the answer haha
Your answer
Follow this Question
Related Questions
How do I make the character rotate with the 2D camera? 0 Answers
How to delay render one camera for X ms? 1 Answer
Unity 3D: Third person movement and mouse camera control 0 Answers
Problems with rotation after zoom (camera like Sketchfab) 0 Answers
Problem with follow-up camera with curved world shader 1 Answer