• 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
2
Question by Mattivc · Feb 08, 2010 at 11:29 AM · javascript-specific

Call a function in a different script, using javascript.

How can i in script A call a custom function in script B, using javascript?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by AquaGeneral · Feb 08, 2010 at 12:10 PM

Try t$$anonymous$$s.


Script A

var ScriptB : ScriptB; //T$$anonymous$$s should be were Script B goes

function Update () { ScriptB.Test(); }


Script B:

var test : int = 0;

function Test () { test++; }


In t$$anonymous$$s example Script A tells Script B to add 1 to the 'test' integer ever frame.

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 Mattivc · Feb 08, 2010 at 12:51 PM 1
Share

So the syntax is: "nameOfOtherScript.nameOfFunction();" ?

avatar image AquaGeneral · Feb 08, 2010 at 01:00 PM 0
Share

Spot on. With this approach it is in theory (at least) quicker than the GetComponent method, as it doesn't have too look for anything on the initial run.

avatar image superluigi · Aug 10, 2015 at 01:23 AM 0
Share

what do we put after we name the variable scriptB?

 var ScriptB : What goes here?;

avatar image Craftnime · Aug 23, 2015 at 10:30 AM 0
Share
 var ScriptB : MonoScript;
avatar image binteimran11 · Sep 13, 2015 at 05:42 PM 0
Share

I am trying this for two cubes and my scripts are: cube1.js var cube2:cube2; function Start () {

}

function Update () {

cube2.rot();

}

cube2.js function rot() { if (Input.GetKey(KeyCode.A)) transform.Rotate(Vector3.up); } but its not working please tell me why it is not working?

avatar image
0

Answer by efge · Feb 08, 2010 at 12:48 PM

Use the function GetComponent() to find a script attached to a GameObject.

Reference and Examples

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
0

Answer by SergeyLavren · Jan 01, 2012 at 04:33 PM

  1. You can use Component.GetComponent. But it will work only if the target script is attached to your object or you need to have reference to another object to invoke GetComponent on it.

  2. You can create static methods in your script and invoke it from anywhere .

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
0

Answer by Pondomaniac · Mar 27, 2015 at 02:14 PM

Helle i know t$$anonymous$$s post is old, but the best practice is to use "BroadcastMessage" to call a methode name on all gameobjects

https://docs.unity3d.com/ScriptReference/Component.BroadcastMessage.html

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 tanoshimi · Mar 27, 2015 at 02:17 PM 0
Share

Why are you proposing a new answer to a 5yr old question that's already been answered with the "best practice" answer? (to create a public reference to the other script)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Input.GetButtonDown not working at ALL 1 Answer

"Can't find namespace" - accessing Javascript from C# (or C# from Javascript) 3 Answers

Access a static var from different named scripts? 1 Answer

Reload Level on Collision 4 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