Is there a way to put a function somewhere so that every script can have access to it?
like
mainfunction(){}
but scriptA,scriptB,scriptC
can all call
mainfunction();
?
Thanks,
Dan
Is there a way to put a function somewhere so that every script can have access to it?
like
mainfunction(){}
but scriptA,scriptB,scriptC
can all call
mainfunction();
?
Thanks,
Dan
There are a few ways to do this but i like to use a static bool.
static var mainFBool : boolean;
function Update() {
if(mainFBool==true) {
MainFunction();
}
else {
mainFBool = false;
}
}
MainFunction(){
//Do Whatever.
}
To call it from elsewhere
function Update() {
if(//This condition is met) {
ThatScriptsName.mainFBool = true
}
And I think you can jus call it like this too
function Update() {
if(//This condition is met) {
ThatScriptsName.MainFunction()
}
Not sure about the second one cause i never use it that way lol. but its worth a try. ![]()
And there are static functions as well. :idea:
Declare it as static, then you can access it like so:
scriptName.FunctionName();
Thanks but I’m running into even more problems.
I have one script called joints.js
it has info for setting up joints with functions like
createJoint(){
//dostuff
lockAxis()
}
function lockAxis(){
//dostuff
}
Now I want to access these from
main.js
so in main.js I have
joints.createJoint();
Errors: no instance of joints
Errors: LockAxis cannot be called because it is not static. Lockaxis is called by createJoint which is static?
Anyway I’m running into major trouble. Could you tell me how to create an instance of joints and why lockaxis no longer works in this example.
Thanks,
Dan
This put in main
var targetscript : joint;
then link in the inspector the joint.js in that variable then in main instead of this :
joints.createJoint();
do this:
targetscript.createJoint();
anyway I think thats right
actually hmm not sure if this will work, I know if you put the object with the script it might work.
that seemed to work. It did not seem necessary to declare them as static.
?
No its not necesary to declare them as static but I think if you do then any script or any object can use them. Not just the object that has the link to it in the inspector.
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
–Eric
–Edit
Now it’s working. I attached the joints.js to the Main game object and that worked.
Now it’s not working again.
I have a script called jeep.js which needs to use functions in joints.js
in jeep.js I have
var jointscript:joints;
Debug.Log(jointscript) is null
I can’t link it in the inspector because the jeep script is added on the fly during runtime. So I need some code to create an instance of the joints.js?
joints.js is not attached to anything at the moment. Even if I attach joints.js to my main object “Main” then it still cannot find it.
I also tried accessing it this way.
jointscript=GameObject.Find(“Main”).GetComponent(joints); putting joints in quotes or not still doesn’t work.
Anyway it’s not working.
Declare the global functions in the joints script as static:-
static function MyFunc() {
...
}
…and then use the name of the script itself to qualify the function name:-
joints.MyFunc();
Anyway I got it to work by attaching all the scripts to one object a Main GameObject that has nothing else but the scripts.
var jointsscript : joints;
jointsscript=GameObject.Find(“MAIN”).GetComponent(joints);
When I declare a function static in joints.js then it can no longer find other functions in the same file?
like if createjoint needs lockaxis then do they all have to be static?
Thanks,
Dan