BroadcastMessage variable passing issue

I have a parent object which I want to have send information to it’s children objects which determines their movement, which looks like this:

gameObject.BroadcastMessage("receiveDirection", minXRange, maxXRange);

And the corresponding function in the child and the object sending the message:

function receiveDirection(minXRange : float, maxXRange : float)

However, when I try this, I get the following error:

“No appropriate version of ‘UnityEngine.GameObject.BroadcastMessage’ for the argument list ‘(String, float, float)’ was found.”

Meanwhile, everything works fine when I use:

gameObject.BroadcastMessage("receiveDirection", minXRange);

Is this the expected behaviour? Can I only send only one object/variable as part of BroadcastMessage (not counting it’s sending options, of course) or have I made a mistake?

Thanks.

Correct - you only get 1 passable argument. However - it is of type Object so that means you can roll your own class as a container for all your args and pass that around instead.

Thanks for that Kelso!
Finally, I opted for passing through a string variable and then using conditions to determine the requirements! There’s a little too much code for my liking, but it gets the job done nicely.

You would be a lot better off doing what KelsoMRK said about using a class, rather than trying to parse strings.

–Eric

Why not just pass an array?

gameObject.BroadcastMessage("receiveDirection", [minXRange, maxXRange]);

Eric, Kelso’s solution is a good one, but as it stands now I’m passing strings that are simple, understandable and the result is effective, it just requires more code and I’ve not yet begun to create custom classes (am still just learning and familiarising myself with javascript), so I’ll make creating custom classes my next big challenge =)

Tonyd, that is actually quite superb! I’ll start playing with that right away.

Thanks very much to all of you!

Okay, so I went back and fixed up my code and ended up rolling my own class for this, as per the suggestion of KelsoMRK and Eric5h5 (and ironically found one of Eric5h5’s old posts on the matter, here : http://forum.unity3d.com/threads/12090-Defining-structures-in-unity-javascript )

So, for anyone who stumbles across this needing to do what I did:

I made a custom class to store four variables and gave it a constructor so I could assign the variables more easily and with less code:

class Vector4
{
	var minX : float;
	var maxX : float;
	var minY : float;
	var maxY : float;
	
	function Vector4 (f1 : float, f2 : float, f3 : float, f4 : float)
	{
		minX = f1;
		maxX = f2;
		minY = f3;
		maxY = f4;
	}
}

And made a new variable of that class:

var bottomDirection = new Vector4(-3, 3, -0.5, -2);

And now I have the broadcast message reading as such:

gameObject.BroadcastMessage("receiveDirection", bottomDirection);

And, finally, the receiving function reads in the data and parses it as needed:

function receiveDirection(direction : Vector4)
{
	xMovement = Random.Range(direction.minX, direction.maxX);
	yMovement = Random.Range(direction.minY, direction.maxY);
}

Many thanks again, KelsoMRK, Eric5h5 and Tonyd! This really helped me clean up my code :slight_smile: