- Home /
How To Call a function from an other Script
Hi everyone,
I finished my big Script page about my dialogue system. And there is in this DialogueScript a main function with many arguments (like classes(that returns GameObjects, tabs of textures, booleans... ),variables etc...) which have also other function inside. This work nicely when I call my main function on same script's function start. But, I want to call this main dialogue function from other GameObjects. (for me it would be clearer, and easy to put specific dialogue from a specific npc)
To see what happened if I call my function from other script, I didn't put a specifique interact. So I wrote this on the NPCScript : (Sorry I'm Frensh so, some variables and classes are in frensh word... ^^' hope you understand a little)
//NPCScript
var ScriptObject : GameObject;
function Start () {
var Scripts = ScriptObject.GetComponent("DialogueScripts") ;
Scripts.dialogueN(EtatDialogue.debut,false,false,"",Scripts.backgrounds[0],"Salut mec !","Il fait beau aujourd'hui !","Tu veux jouer avec moi ?",new PersoDialogue(Personnage.ten,TypeEmotion.content,"Ten",true,true),PortraitTrans.tVersDroiteEntreeG,new PersoDialogue(Personnage.personne,TypeEmotion.normal1st,"",false,false),PortraitTrans.apparition,new PersoDialogue(Personnage.personne,TypeEmotion.normal1st,"",false,false),null);
}
So my questions are : How to call my function with arguments that are from other script without copy and past all classes, variables etc... on the NPC script ? Do I have to put into each argument a "Scripts.", or maybe something else ?
I tried many things... And still not working. I have always this issues :
BCE0005: Unknown identifier:'backgrounds'. BCE0005: Unknown identifier:'dialogueN'.
I wish you a nice day, and thank you in advance to help me.
Friendly,
Golden.
Answer by Golden · Nov 30, 2012 at 08:53 AM
Ok ok, i'm dumb... In my NPCScript I have always changed only my first line instead of all functions calls... (yes because I called many time my function to have a interveiw between 2 characters) , I've just forgot to add other calls function with "Scripts." So now It's working perfect ! May my question and my answer be usefull for everybody. Ashamed
Answer by azmat786n · Nov 30, 2012 at 09:07 AM
script 1
static function myFunction () {
doSomething()
}
script 2
function Update() {
if(Input.GetMouseButton(0)) {
script1.myFunction();
}
}
mmmm, could work but I think he said that he has many game objects with that dialogue thing. Static won't do here. On top of that what if the function needs access to non-static variables? By the way he said he found the issue about 20 $$anonymous$$utes ago.
Answer by CuddleMonster · Nov 30, 2012 at 08:56 AM
You are trying to send Variables inside the function's brackets? functionname ( var1, var2, var3 )?
you can only send one variable in a function, unfortunately. but you can send an array containing many variables.
//Calling it
var DialoguePackage = new Array( String1, Integer1, String2, Transform1 );
function Dialogue ( DialoguePackage );
// the function itself
function Dialogue ( DialoguePackage : Array ) {
//here you can use your previous variables but by their "packed" name.
var ExampleVariable : int = 5 + DialoguePackage[1];
}
???? What? you can only send one variable (aka parameter) to a function?? So this function below won't work then...
Vector3.Distance(vec1, vec2);
Damn I have been using a few times already....
And what is this array you are creating?? Do you mean ArrayList??
As far as I know you cant send more than one var in a function. I had to send Arrays for when I built a server sided Damage/Health script.
the Array here is slightly different from ArrayList. It is called a Javascript Array. It is more efficient than an ArrayList (I could be wrong) and its downside is it cannot be resized. (no need to resize it in this situation) http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
$$anonymous$$Y BAD! I was wrong. I was thinking of "Send$$anonymous$$essage" in which you cannot have more than one parameter.