- Home /
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?
Take a look at Events and Delegates:
http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html
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#
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
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?
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
Follow this Question
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