- Home /
siple question...how to comunicate with other script
Hi just for learn ... I have this script:
//OpenLinkURL.js
var insertLink:"http://www.google.it/";
static function openLink() {
Application.OpenURL (insertLink);
}
and I would to use it by this script:
//GUIObject.js
private var Link:OpenLinkURL;
if(GUI.Button(20,20,50,50,"Link")){
Link.openLink();
}
it doesn't work...how can I use the result of openLink function on the second script? thanks!
Answer by Grady · Jan 04, 2012 at 08:52 AM
Hey, what I would have is something like this:
//OpenLinkURL.js var url = "http://www.google.com/";
function OpenLink(){ Application.OpenURL(url); }
And then in your other script, have this:
//GUIObject.js var objectLinkScriptIsAttachedTo : GameObject;
functin OnGUI(){ if(GUI.Button(20,20,50,50,"Link")){ objectLinkScriptIsAttachedTo.GetComponent(OpenLinkURL).OpenLink(); } }
What is happening in the first script is obvious, what is happening in the second is, we have to drag the object that the OpenLinkURL.js is attached to, so that we can then get the component (which is the actual script) from that object, and tell it to do the OpenURL()
function....
Here is the Script Reference for GetComponent()
If you need any more help, then feel free to comment back!!!!!!!!!!
-Grady
thanks for help Grady!! ...but it doesn't work :( I have fixed a couple of syntax errors from OnGUI function...
function OnGUI(){ if(GUI.Button(Rect(20,20,50,50),"Link")){ objectLinkScriptIsAttachedTo.GetComponent(OpenLinkURL).OpenLink(); } }
maybe I need to pass the result of a function not a function ... am I wrong? thanks!
it works! .. Did you drag in the object in the inspector?????? ehm...NO ...hahaha......sorry for stupid mistake Grady :) ...thanks so much!!
Answer by lcn75 · Jan 04, 2012 at 02:41 PM
I have fixed a couple of syntax errors from OnGUI function...but It doesn't work ...
// OpenLinkURL.js
var setURL = "http://www.google.com/";
function OpenLink(){
Application.OpenURL(setURL);
}
//GUIObject.js
private var objectLinkScriptIsAttachedTo : GameObject;
private var condition = true;
function OnMouseDown(){
condition = false;
}
function OnGUI(){
if(!condition){
if(GUI.Button(Rect(600,20,50,50),"Link")){
objectLinkScriptIsAttachedTo.GetComponent(OpenLinkURL).OpenLink();
}
}
}
console display this error: "NullReferenceException GUIObject.OnGUI () (at Assets/Scripts/GUIObject.js:11)"
Answer by mpavlinsky · Jan 04, 2012 at 09:14 PM
How are you assigning "objectLinkScriptIsAttachedTo" to the object you want? If it is private in the script it's impossible to be linking it via the editor.
infact! :) ... I have erased private, assigned the object in the inspector and now it works! thanks!
Answer by by0log1c · Jan 04, 2012 at 10:17 PM
The information you need is in the Unity Scripting Reference: accessing GameObjects, accessing Components. There's MANY way to do it, though in that specific case, I'd probably make the function static, to be accessible from anywhere, like this:
//OpenLinkURL.js
private var setURL:String = "http://www.example.com";
public static function OpenLink():void
{
Application.OpenURL(setURL);
}
//GUIObject.js
if(GUILayout.Button("Open Link"))
{
OpenLinkURL.OpenLink(); //ComponentName.StaticFunctionName();
}
Remember that all JS scripts are actually public class, hence you can access OpenLinkURL like you would a fully declared class.
Answer by lcn75 · Jan 05, 2012 at 08:28 AM
it works! .. Did you drag in the object in the inspector?????? ehm...NO ...hahaha....thanks so much!! here the scripts...
// OpenLinkURL.js
var setURL = "http://www.google.com/";
function OpenLink(){
Application.OpenURL(setURL);
}
//GUIObject.js
var objectLinkScriptIsAttachedTo : GameObject;
private var condition = true;
function OnMouseDown(){
condition = false;
}
function OnGUI(){
if(!condition){
if(GUI.Button(Rect(600,20,50,50),"Link")){
objectLinkScriptIsAttachedTo.GetComponent(OpenLinkURL).OpenLink();
}
}
}
after assign two scripts to the object on scene, drag the object from Hierarchy panel to the "objectLinkScriptIsAttachedTo" tag. Thanks to all!! bye.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The global function. 1 Answer
Custom Global Functions 1 Answer
Static function and variables error 2 Answers
Accessing non static members in a static function argument?? 1 Answer
Adding static functions 2 Answers