Hello.
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);
}
SendNewMessage();
}
}
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++){
gos*.SendMessage("CheckPosition",hitpos1,hitpos2);*
}
}
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.
Thanks.