"Cannot cast from source type to destination type"

I’m working on an RTS game and I need some help. I am writing a code which would allow me to select units by dragging a box with mouse and selecting the units. I’ve got most things figured out, I can drag boxes and it gets the positions, I just need to select the units that are in those positions. My idea is that I will send a message to all objects with a tag “Unit” with positions that I got from the drawn box so the objects can check themselves if they are inside of the selection and accordingly change boolean to be on or not. From all the google-ing I did, I found a code which should select all objects with tag “Unit”, but everytime I try activating the function, it gives me error about the SendMessage

InvalidCastException: Cannot cast from source type to destination type.
Mouse.SendNewMessage () (at Assets/Scripts/Mouse.js:53)
Mouse.Update () (at Assets/Scripts/Mouse.js:42)

Here’s my script, it also has code which allows to draw boxes and get positions.

var ClickAnimation : GameObject;
var StartRectX :float;
var StartRectY :float;
var EndRectX :float;
var EndRectY :float;
var EndRectX2 :float;
var EndRectY2 :float;
var SelectionSkin : GUIStyle;
var hitpos1;
var hitpos2;
function Update () {
	if (Input.GetButtonDown ("Fire2")) {
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit : RaycastHit;
		if (Physics.Raycast (ray, hit, 100)) {
			var hitpos = Vector3(hit.point.x,0,hit.point.z);
			Instantiate (ClickAnimation, hitpos , Quaternion.Euler(0, 0, 0));
	if (Input.GetButtonDown ("Fire1")) {
		StartRectX = Input.mousePosition.x;
		StartRectY = Screen.height - Input.mousePosition.y;
		var ray1 = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit1 : RaycastHit;
		if (Physics.Raycast (ray1, hit1, 100)) {
			hitpos1 = Vector3(hit1.point.x,0,hit1.point.z);
	if (Input.GetButton ("Fire1")) {
		EndRectX = StartRectX-Input.mousePosition.x;
		EndRectY = StartRectY-(Screen.height - Input.mousePosition.y);
		EndRectX = EndRectX-EndRectX*2;
		EndRectY = EndRectY-EndRectY*2;
	if (Input.GetButtonUp ("Fire1")) {
		var ray2 = Camera.main.ScreenPointToRay (Input.mousePosition);
		var hit2 : RaycastHit;
		if (Physics.Raycast (ray2, hit2, 100)) {
			hitpos2 = Vector3(hit2.point.x,0,hit2.point.z);

function SendNewMessage(){
		Debug.Log("Message Sent with"+hitpos1+" and "+hitpos2);
        var gos : GameObject[];
        gos = GameObject.FindGameObjectsWithTag("Unit"); 
        for(var i = 0; i<gos.length; i++){


function OnGUI() {

  • if (Input.GetButton (“Fire1”)) {*
  •   GUI.Box(Rect(StartRectX,StartRectY,EndRectX,EndRectY),"",SelectionSkin);*
  • }*
    My units just have a script that has Debug.Log to show that it has recieved the message.
    Any help is appreciated, I don’t have much experience with SendMessage so I have no clue how to fix this.

The docs that you have read :wink: say that the third argument to SendMessage() is a SendMessageOptions type. I guess the compiler is having problems working out how your un-typed hitpos2 can be converted into one of those. You seem to use hitpos2 as if it was a Vector3.

And this is where c# and strong typing come in, at least you would have got a better error message :slight_smile:

SendMessage is limited to sending one object as a parameter. The third parameter must be a SendMessageOptions. The way to get around this is to group your parameters into a class or [struct][1]. However I’m not going to show you how to do this, google is your friend here.

Instead I would suggest invoking the method directly. SendMessage uses reflection and is really only worth the effort if there may be more or less then one component on the receiving gameObject that is interested in the message. Sample code (C#):


[1]: Structure types - C# reference | Microsoft Learn