Sending message to one object

Hi, in my game, I want to start a function in a certain object multiple times, from multiple different events, is there anyway to SendMessage to something like a global function that can be triggered easily by multiple events? I use c# btw

Thanks in advance

1 Answer

1

Check out delegates in C#. They’re a super useful way to register functions to other classes so that you can decouple scripts from each other.

On Unity community wiki there is a really great messenger broadcast system that uses the delegate type to create a system to listen for broadcast messages that is much faster than unity’s built-in SendMessage method.

It might take a minute to wrap your head around if you haven’t used delegates or function pointers before, but you should consider taking the time to work it out. It really helps solve quite a lot of problems.

Now, like any awesome tool, learn it’s shortcomings and don’t abuse it

:smiley:

Update

I just found this handy tutorial via Unity (which I haven’t watched yet, but looks like exactly what you need)

Where exactly to write the first line?