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 BobbyDoogle · Mar 11, 2014 at 05:50 AM · c#function

C# Call a funtion in many scripts

Hi, I am looking for a way to call a differentiated method in many scripts (it is related to saving variables for the object the scripts are attached to). Effectively I would like to have one script call out the save, and then many different scripts to have different save methods that are executed (as I want to save/load different information for these objects).

I don't want to have to to declare each game object and each script in the calling script, which is the way I am used to. With standard declaration I would have hundred of scripts to declare, and it would make adding new objects a chore. In plain English something like:

all scripts containing method mySaveMethod () execute that method.

in a similar fashion I will then want to execute:

all scripts containing method myLoadMethod() execute that method.

In looking through forums a possibility seems to group everything under a gameobject then use broadcast message. Is this a recommended, or elegant way?

Comment
Add comment · Show 1
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 robertbu · Mar 11, 2014 at 05:53 AM 0
Share

Take a look at Events and Delegates:

http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Mar 11, 2014 at 05:58 AM

Well, I would say events are what you're looking for. You would have to have the one object fire an event and every object that is listening to that object would hear the event and react accordingly, but you would still have to declare the delegate script within the script that is listening because, otherwise it would never 'hear' the event.

Another option would be to tag all of the objects that need to do the function with a certain tag and iterate through the tagged object with FindObjectsWithTag, but I think this would be slower and less elegant than what I mentioned above.

I have heard that broadcast message is slower than events in C#

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 BobbyDoogle · Mar 11, 2014 at 04:52 PM 0
Share

Thanks, I believe this is exactly what I needed. At this point I just have a very simple event dispatcher and listeners doing debug.log's of the listeners positions, but I think it will be fully expandable. I thought I would post a helpful link I found researching events and handlers for anyone else stumbling upon this post:

http://www.everyday3d.com/unity3d/events/EventDispatcher.cs.txt

avatar image BobbyDoogle · Mar 12, 2014 at 02:20 AM 0
Share

So this is working great, I do have a follow up question though. Is it efficient for a a large number (lets choose 100 to make that objective) of objects to have listening functions, that receive a save/load call from the master save/load script? Do listeners use much resources when they are not being called?

avatar image highpockets · Mar 12, 2014 at 02:50 AM 0
Share

I can't tell you exactly how efficient it is, but to the best of my knowledge, it is the most optimized approach. You could test out the efficiency by making 100 prefabs which have a listener script attached and keep increasing the amount of prefabs while you have an object moving for example to see when you notice a lag when you fire the event.. Also, if you think my answer does the trick, mark it as accepted with the tick mark at the top left of my answer. This way, when people are searching for an answer like this in the future, they know this is a correct answer.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity C# Start function without extending Mono 2 Answers

How do you call a static function in C# from Android? 1 Answer

what is wrong whis this? 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