A
A
Alexey2017-10-19 18:53:23
C++ / C#
Alexey, 2017-10-19 18:53:23

Use SendMessage or is there an alternative?

I was interested in the question of whether to use SendMessage, I found information on one forum that it is slower than GetComponent. They also suggested using an alternative event system
wiki.unity3d.com/index.php/Advanced_CSharp_Messeng... In
general, I liked the system. A single method can be called from anywhere without accessing the component directly.
But this system has one disadvantage: you cannot call all methods with the same name. And SendMessage allows you to call all methods with the same name inherited from MonoBehaviour.
Actually, what I need this for: when one method has finished its work, it tells all the methods that are waiting for it to finish that it has finished working and can pass some parameters to all these dependent methods.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2017-10-19
@Alexeee

You need classic events.

class Enemy{void Start(){Player.Spawn+=StartAttack;}}
class Player{event someDelegate Spawn;}

G
GavriKos, 2017-10-19
@GavriKos

The alternative to SendMessage is the right project architecture.
And yes, I wouldn't use SendMessage in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question