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 /
avatar image
0
Question by jessee03 · Jun 16, 2011 at 04:26 PM · functions

calling functions from any script ?

I'm having trouble calling a function from any script I want. It seems like everything in the script that's going to be called must be set to a static. I'm very confused I cant just call a function from a different script and then have it call another function inside of that other script. Any easy way of doing this ?

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

2 Replies

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

Answer by Eric5h5 · Jun 16, 2011 at 04:46 PM

Nothing has to be set to static, and shouldn't be unless that's what you actually want (which it frequently isn't). See the docs about accessing other game objects.

Comment
Add comment · Show 4 · 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 jessee03 · Jun 16, 2011 at 05:11 PM 0
Share

I keep getting a nullReferenceException when I try accessing my function through the first code on the page.

avatar image Eric5h5 · Jun 16, 2011 at 05:27 PM 0
Share

That means you're trying to refer to an object which doesn't exist.

avatar image jessee03 · Jun 16, 2011 at 05:53 PM 0
Share

nvm needed to use GameObject.Find("$$anonymous$$ain Camera") and now it works great !

avatar image Eric5h5 · Jun 16, 2011 at 06:05 PM 0
Share

You can use Camera.main ins$$anonymous$$d of that.

avatar image
3

Answer by testure · Jun 16, 2011 at 04:34 PM

 OtherScriptClass otherscript = transform.GetComponent<OtherScriptClass>();
 otherscript.FunctionToCall();

easy as that.. though in order for it to work, you need OtherScriptClass assigned to the gameObject as a component.

Now, since you didn't say what language you're using I'm going to assume that you use what I use (C#). If you're using Javascript or Boo, then consult the unity refs and figure out how to make it work with your language (it's pretty easy).

You mentioned making something static- that's only for something that is not going to change during runtime (hence static). You can assign a script reference to a static variable and make it accessable to any script at any time- but it's only useful for things that are singular in nature (such as a camera controller, or gamestate manager, something like that).

In those cases, yes- you would use the static keyword and make it a publicly accessable member variable like this:

 class MyClass : MonoBehaviour    
 {    
     public static MyClass Instance;    
     
 
     void Awake()    
     {    
         Instance = this; // you can now call MyClass.Instance.Function() from anywhere in any script        
     }    
     
 
     public void Function()    
     {    
         Debug.Log("Hey, it works");    
     }    
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Referencing functions from other scripts as variables 4 Answers

JavaScript - Reset Characters Position 2 Answers

Finding all public functions on a game object 1 Answer

Can't use GetComponent (Calling JS in C#), and don't know why 1 Answer

Calling function found on hundreds objects(best way?) 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