- Home /
time
What is the best way to call a function from another script
I keep running in to this problem with send message and such because sometimes it works sometimes it doesn't work. All i need is some demonstrations of ways to use send message in a better way or eny other thing that is simpler to call a function from one script to a different one.
The most efficient way would be to implement events. You can broadcast events and all subscribers to those event will listen and act accordingly. They are preferable because of low overheads and easy script maintainability and organization.
Here is the official unity tutorial on events.
Answer by JamieSinn · Nov 04, 2013 at 02:07 AM
In c# it would be to make the method public or static or even both.
eg... public static void SomeMethod() {
}
yes i have it as public but what is the best way to set up a sendmessage?
Why do you insist on using Send$$anonymous$$essage? It is probably the worst option of them all and unless you have a good reason for using it, you can easily avoid it. If your methods are public, you can simply call them from wherever you want.
Now as mentioned by the commenter above, depending on your specific use case, you may be looking for a way to broadcast events to several objects, and in this case it would probably be best to use events.
Follow this Question
Related Questions
Why is calling a function wrong? 2 Answers
call function with same name + number 1 Answer
How can I call a method from another script? 3 Answers
Sending a message from javascript to C# 1 Answer
Multiple Cars not working 1 Answer