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 Symyon · Mar 30, 2013 at 10:41 AM · typecastingscript referencescript referencing

How to use methods with the same name from different scripts attached to this script with a reference?

How do I reference a generic script in another script? So lets say Object AO has script AS with method name "Method()". Another object BO has a script BS with the same name "Method()". Now, I want to have a script SS which will be attached to both AO and BO and have a reference to AS and BS respectively via the Unity editor. From that SS script I will do a logic which will use "Method()" name. The problem here is that I want to do in SS script:

 public GameObject ao_or_bo_script;
 
 ao_or_bo_script.Method(); 

but that will work only if I'll do a type cast like

 (AO)ao_or_bo_script.Method();

 

or

 (BO)ao_or_bo_script.Method();
 

which doens't work for me because I don't know wich script I'll attach

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

Answer by whydoidoit · Mar 30, 2013 at 10:42 AM

So you have three choices:

  • SendMessage (BroadcastMessage) which will call that method on every script attached with a matching signature

  • Inheritance - create a base class with a virtual Method, override it in the two A/B scripts, cast your a_or_b_script to the base class and call the method on that.

  • Interfaces - create an interface with the method, implement it on both scripts, cast the reference to the interface and call it with that.

Note that the GameObject isn't actually the scripts - so if you don't use SendMessage then you will have to find the components derived from the base class using GetComponent or the interface by searching through the available components.

Comment
Add comment · Show 2 · 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 Symyon · Mar 30, 2013 at 10:45 AM 0
Share

I'll avoid Send$$anonymous$$essage() from efficiency standpoint. But thank you for the Inheritance advice!

avatar image whydoidoit · Mar 30, 2013 at 10:59 AM 0
Share

Yep it's slow. 100x a function call - but that's usually ok for events - just not anything else! The version of GetComponent that takes a type (not the generic version with the ) can find interfaces BTW.

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

11 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

Related Questions

Instantiate Typecasting 4 Answers

Classes and subclasses typecasting in unityscript 0 Answers

Direct reference to a hierarchy object from prefab? 2 Answers

Type casting error while instantiating. 1 Answer

Dynamic type, unknown in advance to reuse scripts 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