• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by lcn75 · Jan 04, 2012 at 08:32 AM · functionglobalurl

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lcn75 · Jan 04, 2012 at 12:23 PM 0
Share

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!

avatar image Grady · Jan 04, 2012 at 03:00 PM 0
Share

Did you drag in the object in the inspector??????

avatar image lcn75 · Jan 04, 2012 at 04:22 PM 0
Share

yes! ... nothing happens!

avatar image Grady · Jan 05, 2012 at 12:31 AM 0
Share

try ins$$anonymous$$d of (OpenLinkURL)

avatar image lcn75 · Jan 05, 2012 at 08:11 AM 0
Share

it works! .. Did you drag in the object in the inspector?????? ehm...NO ...hahaha......sorry for stupid mistake Grady :) ...thanks so much!!

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lcn75 · Jan 05, 2012 at 08:21 AM 0
Share

infact! :) ... I have erased private, assigned the object in the inspector and now it works! thanks!

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Static function and variables error 2 Answers

Accessing non static members in a static function argument?? 1 Answer

How do you achieve variables and functions that are global between scenes? What is the BEST way? 1 Answer

Adding static functions 2 Answers

function is faster when accessing function variables not global ones? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges