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!

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

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)”

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.

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.

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.