Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by CezaryDevelopment · Nov 19, 2015 at 02:52 AM · functionvariables

I want to run a function in another script in another object.

Basically I want to run a test function in my Enemyscript.js file which is located in the Enemy object from my Bullet_Movement.js located in a totally separate object, Bullet. I just want to know how to run the simple function, then Ill modify it myself.

Enemyscript.js: #pragma strict

 public var Health : float = 100f;
 
 function Start () {
 
 }
 
 function Update () {
 
 }
 
 function RemoveHealth () {
     Debug.Log(1 + 1);
 }

Bullet_Movement.js: #pragma strict

 public var speed : float = 10f;
 public var Enemy : GameObject;
 
 private var myOtherClass : AnotherClass;
 
 function Start () {
     myOtherClass = new Enemyscript();
     myOtherClass.RemoveHealth();
 }
 
 var thePositionToMoveTo : GameObject;
  
 function OnCollisionEnter2D(other : Collision2D) {
     Debug.Log("Hit");
     if(other.gameObject.tag == "Respawn") { //Or whatever
         gameObject.transform.position = thePositionToMoveTo.transform.position;
     }
     
 
 }
 
 
 function Update () {
     transform.Translate(Vector2.up * speed * Time.deltaTime);
 }
 
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

1 Reply

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

Answer by ZefanS · Nov 19, 2015 at 08:04 AM

In general if you have two GameObjects, each with a script attached, you can call one script from the other by getting a reference in one script to the other script:

Script1.js attached to GameObject1 - we want to call HelloWorld():

 #pragma strict
 
 function HelloWorld()
 {
     Debug.Log("Hello World");
 }

Here are a few ways to reference and call HelloWorld() from Script2.js attached to GameObject2:

Declare GameObject1 as a public variable and assign it in the Inspector, then use GetComponent:

 #pragma strict
 
 public var gameObject1 : GameObject //Assign GameObject1 in the Inspector
     
 function CallHelloWorld()
 {
     gameObject1.GetComponent.<Script1>().HelloWorld();
 }

Declare GameObject1 as a public variable and get a reference at runtime, and again use GetComponent:

 #pragma strict
     
 public var gameObject1 : GameObject;
 
 function Start()
 {
     gameObject1 = GameObject.Find("GameObject1");
 }
 
 function CallHelloWorld()
 {
     gameObject1.GetComponent.<Script1>().HelloWorld();
 }

Get the entire reference at runtime when you wish to call the function:

 #pragma strict
     
 function CallHelloWorld()
 {
     GameObject.Find("GameObject1").GetComponent.<Script1>().HelloWorld();
 }

Declare Script1 as a variable and get a reference to it directly in one of the same ways, eg:

 #pragma strict
 
 public var script1 : Script1 //Assign in the Inspector
 
 function CallHelloWorld()
 {
     script1.HelloWorld();
 }

The method you'll use will depend on the particular scenario. For instance, if you are trying to access a script attached to a GameObject that gets instantiated at runtime, then you won't be able to assign the variable in the Inspector and instead will need to get the reference to the script after the object has been instantiated. However, if the GameObject exists from the start of the Scene, it may be simpler to just assign it in the Inspector.

Hope this makes things a bit clearer.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to update variable from function input 0 Answers

Updating a global variable from another script 0 Answers

OnPointerExit getting called when you click on a gameObject with the interface implemented. 0 Answers

How to get rid of this glossy effect that appears on all the assets ? 0 Answers

How do I get all of my code in start function 1 Answer


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