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
4
Question by john 9 · May 11, 2011 at 12:12 PM · access

How do you access a script attached to another object

The problem is i need to access an animation script from another script how do i do that???

Please help i dont think there is any need to show an example as it is a simple question well i think it is!

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
8
Best Answer

Answer by Cameron 3 · May 11, 2011 at 05:37 PM

I use C#, but usually what I do is:

1) Add a public variable to the script that wants to access the other script. So for example, if I have a script called "Death" and I want to access "MyAnimationScript", which is attached to some game object that is already instantiated in the editor, i can do:

public class Death : MonoBehavior {

 public MyAnimationScript someScript;

 void Start () {

 }

 void Update () {

 }

}

2) If my death script is attached to some game object in the editor, that game object will have a slot called "someScript" and I can drag the game object with my animation script on top of it.

Alternatively, you can search for the other script using GameObject.FindGameObjectWithTag and GetComponent. So, if my Animation script was on a game object that had a tag called "PlayerAnimation", I could do:

public class Death : MonoBehavior {

 private MyAnimationScript someScript;

 void Start () 
 {
       someScript = GameObject.FindGameObjectWithTag("PlayerAnimation").GetComponent<MyAnimationScript>();
 }

 void Update () {

 }

}

Now I can access that script and all of it's variables in my Death script by using

someScript.whatever

Comment
Add comment · Show 3 · 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 john 9 · May 11, 2011 at 08:42 PM 0
Share

How would you write that in java haha sorry

avatar image Akeronz · Aug 03, 2013 at 02:41 AM 0
Share

Thanks for the answer Cameron3! A lifesaver :)

avatar image Lord_Ford · Jun 14, 2015 at 07:31 PM 0
Share

Thank you very much. -Have a great day and cheers! :D

avatar image
8

Answer by CHPedersen · May 11, 2011 at 12:23 PM

ScriptThatYouWant = GameObject.Find("[GameObjectName to which target script is attached]").GetComponent<[Name of target script]>();
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 john 9 · May 11, 2011 at 01:06 PM 0
Share

This doesn't seem to work could you more detail am i meant to keep the [] in the script the script i am writing the code in is Die Script and the script i want to access to play an animation is Walk

avatar image john 9 · May 11, 2011 at 01:07 PM 0
Share

if that makes sense to anyone

avatar image CHPedersen · May 12, 2011 at 06:15 AM 0
Share

No, remove the [] (brackets). They simply indicate that you are meant to replace the text with your own classnames and object names.

avatar image ToxicCherry · Nov 05, 2016 at 05:14 PM 0
Share

This should be the best answer. Since Object names are more unique, tags being useful for dynamic object addition and removal.

avatar image thisaraunity · Jan 16, 2020 at 09:17 AM 0
Share

Works very well in my case:

 waveSpanner = GameObject.Find("Wave$$anonymous$$anager").GetComponentInChildren<WaveSpanner>();
             print(" waveCount-->" + waveSpanner.waveCount);
avatar image
0

Answer by FLASHDENMARK · May 11, 2011 at 01:55 PM

I am not completely sure what you are trying to achive, but:

//When using static variables you can access them from other scripts //Call this script TEST. static var yourVariable = 10;

//This is a new script function Update () { (script name) TEST.yourVariable(the variable) += 10 * Time.deltaTime; }

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 john 9 · May 14, 2011 at 12:59 PM

Sorted it thanks guys just added an ontriggerenter to the animation script

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 parmardarshanv · Jan 16, 2020 at 01:11 PM

My personal best way is: Write the following line of code(outside start or update or any methods)

 public static currentscriptname instant

and in the start function of the same script, write:

 instant = this;

If you want to access the script in any other script, write:

 /*scriptyouwanttoaccess*/.instant./*whatever you want to do*/


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 do I access information (eg, variables, functions, properties) on other objects from inside a script? 2 Answers

Very simple component-access question 0 Answers

How can I access other scripts and their functions? 3 Answers

A way to set object's script as a variable of sorts? 1 Answer

How to access variables from another script on collision ? 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