- Home /
Problem calling functions from another script of the same gameObject
I've created a separate script for all my dialogue, and want to call these functions at points in the game. The dialogue script (DialogueScript.js) is attached to my GameController, the same as my GameControllerScript.js and I want to keep an instance to it so that I can use dialogue functions like this:
private var dialogueManager;
function Start() {
dialogueManager = gameObject.GetComponent(DialogueScript);
}
function Update() {
dialogueManager.SayDialogue("Hello there!");
}
But the above doesn't work. However I can do this:
gameObject.GetComponent(DialogueScript).SayDialogue("Hello there!");
But this looks clumsy to me. Am I referencing the other script incorrectly?
$$anonymous$$indly elaborate on
But the above doesn't work.
Also are you calling
gameObject.GetComponent(DialogueScript).SayDialogue("Hello there!");
in your Start function or Update function?
Answer by Malapropos · Jun 13, 2014 at 01:35 PM
It sees dialogueManager as Object instead of DialogueScript. Declare your member variable like this instead:
private var dialogueManager : DialogueScript;
Other than that, your original code should work with the setup you described.
Your answer
Follow this Question
Related Questions
NullReferenceException when trying to link 2 scripts 1 Answer
string problem 2 Answers
Dialogue strategy? 0 Answers
How to prevent gamers from changing the default controls? 1 Answer