Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Reafy · Oct 05, 2014 at 11:42 PM · javascriptfunction

How to use functions between two scripts

OKAY OKAY, before you go getting mad and closing my question i have tried googleing this and none of them really helped me and just left me confused. So i want to call the function on this script #pragma strict

 var Health = 100;
 
 function Update () {
     if (Health <= 0) {
         Dead ();
         // call function here
     }
 }
 function ApplyTheDamage (theDamage : int) {
     yield WaitForSeconds (0.4); 
     Health -= theDamage;
 }
 
 function Dead () {
     Destroy (gameObject);
     Debug.Log("+100");
 }

and then execute it on this script

 #pragma strict
 
 function scoreShow () {
     //execute this function
     Debug.Log("function is initiating");
     gameObject.guiText.enabled = true;
     yield WaitForSeconds(3);
     Debug.Log("yield successful");
     gameObject.guiText.enabled = false;        
 }

 
 function Start () {
     gameObject.guiText.enabled = false;
 }

The two scripts are also on different game objects

Comment
Add comment · Show 2
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 jmparavicini · Oct 06, 2014 at 12:11 AM 0
Share

i dont understand what you want. Be more specific when you ask a question please.

avatar image Reafy · Oct 06, 2014 at 12:26 AM 0
Share

i want to call the function in the first script (the function is scoreShow) and have it executed in the second script (you can see where I put comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cherno · Oct 06, 2014 at 12:28 AM

You just need to have a script variable that references the other gameobject's script, and then you just call the function like this:

 var otherScript;
 
 otherScript.ApplyTheDamage();

You can also use a GameObject variable to reference the whole other GameObject.

 var GameObject otherObject;
 
 otherObject.GetComponent(otherScript).ApplyTheDamage();

How you "populate" the script or GO variables is up to you; for example, you could use GameObject.Find or just drag it to the variable's slot in the inspector.

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 robertbu · Oct 06, 2014 at 12:35 AM

People are rarely mad when closing questions. This particular question gets closed all the time because it has been answered so frequently and in so many different ways. In fact, it rarely makes it through moderation. Here are two references:

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Assuming your other script was named 'Score', and the name of the game object with the script was 'TheScore'. Then you could access the function by:

  GameObject.Find("TheScore").GetComponent(Score).scoreShow();

Note when I did and did not use quotation marks in the above line. That's important. Note for something that will be executed every frame, it would be better to do the Find() in Start() and cache the reference to the script. Or use a public variable for the script and initialize it by drag and drop.

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.

Follow this Question

Answers Answers and Comments

29 People are following this question.

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

Related Questions

Access function (and variable) outside class in the class in Javascript. 1 Answer

yield WaitForSeconds(); issue--very specific, apparently 2 Answers

OnTriggerEnter can't detect my Transform tag 1 Answer

Writing functions that return a value 1 Answer

How do I make the buttons work on this GUI? 2 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