I know this has been discussed before and I know how to do it using an array, but would sending a array be faster than just calling sendmessage twice? or would it be faster to use getcomponent and then just call the function? (This is for bullets and stuff to damage enemies). Thanks.
Sending an array would be faster than 2 SendMessages.
And if you have a reference point for the GameObject, calling GetComponent and the function would be faster than SendMessage.
Ok thanks. What if I dynamically named my enemies ai scripts (like enemyai1, enemyai2, etc…). Would it be faster to just use sendmessage or cycle through an array until it found which ai the enemy had and then call the function from that?
SendMessage is slow.
If you have a manager of some kind that has an array of enemy AIs, use that to access the AIs, it should be faster.
It’s hard to say exactly, and it would be VERY easy for you to just measure it yourself (do it 1 000 000 times in a row, and use Time.realTimeSinceStartup to measure how much time passed). There can be a lot of differences dependent on if you need to use GameObject.Find, Transform.Find, and how exactly you’ve structured everything.
SendMessage is useful for sending generic messages around, but if you’ve got a specific reference and a specific function to call, then doing that avoids searches.
Ok I’ll go test it. Thanks a lot for the help
If your using C# you should try just using a struct.
public struct MessageParameters {
public GameObject arg1;
public Transform arg2;
}
then call it like so:
SendMessage(“OnAct”, new MessageParameters{ arg1 = gameObject, arg2 = other.transform } );
You could use a class too, you can pass any kind of data with messages.
That applies to JS as well.
–Eric
Thanks eric, wasnt sure about JS