Broadcast Message Events

Hi all

I’ve been pouring over pages from the forum and google on things like broadcast messages and events, and arrays. So much so that now I’m more confused than ever.

I have in my game a bunch of gameObjects that spawn and are then moved to a predefined place in the scene. However, whenever they spawn (up to 60 at a time) and move, I get a terrible drop in frame rate that I’m attributing at the moment to the fact that I’m sending out a broadcast message to each of them on spawn. This is what I’m doing:

Item1 = the Prefab of the object being spawned using PoolManager ///
function SpawnMove() {

   for (var item1: Transform in PoolManager.Pools["Item1Pool"])  {
			
	switch(true) { 
		case b_a1 :
                b_a1 = false; item1.BroadcastMessage ("Destination", v_a1); item1.BroadcastMessage ("MoveMe", true);
               break;
		
               case b_a2 :
               b_a2 = false; item1.BroadcastMessage ("Destination", v_a2); item1.BroadcastMessage ("MoveMe", true); 
              break;

   ..... And so forth 58 more cases....

             default:
             break;
   ............etc
}

On the “Item1” prefab gameObject, there is a script that contains:

function Destination(whereAmIGoing : Vector3) {
	this.destinationT1 = whereAmIGoing;	
	
}

So, am I right in thinking that the above method is resource-heavy and would be the cause of the lag (up to 10fps drop!) Also, on the 3Gs and 4, it seems that possibly I’m getting memory leaks with this as well, as the optimal frame-rate of 30fps never returns, and again, I’m thinking that it has something to do with all those broadcast messages.

As mentioned I’ve been looking at events/array lists etc, but they’re not my strong point, and arrays make my head hurt!

Any advice on direction would be appreciated

:slight_smile: :-o

You can try to change the usage of broadcastmessages by using GetComponent and directly calling the wanted method:

Instead of:

item1.BroadcastMessage("Destination", v_a1);

Do this:

var myScript = item1.GetComponent.<ScriptWhichHasTheDestinationMethod>();
if (myScript)
  myScript.Destination(v_a1);

Should be much faster than BroadcastMessage as BroiadcastMessage does some reflection stuff behind the scenes and iterates through all available Components to find the given method by name.

Thanks… I had so happened only just did that before I saw your message. It works but i haven’t tested on the device yet, as I’m worried about getting component on up to 60 items at one time! (60 spawns). I thought that may perform just as badly.

Sure, it will cost you some performance, but much less than working with the massiv reflection effort you have done before that :wink: