Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Thewoxyz · Jun 25, 2014 at 07:30 PM · castsourcedestination

"Cannot cast from source type to destination type"

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 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.
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Jun 25, 2014 at 07:45 PM

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

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. 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#):

 gos[i].GetComponent<myComponent>().CheckPosition(hitpos1,hitpos2);


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Thewoxyz · Jun 26, 2014 at 08:40 AM 0
Share

Thanks, this works, but for some reason unity spits out this error

$$anonymous$$issing$$anonymous$$ethodException: System.Boolean.op_BitwiseAnd

Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.ProduceExtensionDispatcher () I've tracked down that this CheckPosition(hitpos1,hitpos2); causes it. Any clue how to fix that? EDIT: Randomly dissapeard :/
avatar image
0

Answer by Graham-Dunnett · Jun 25, 2014 at 07:33 PM

The docs that you have read ;-) 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Cannot cast from source to destination type 1 Answer

InvalidCastException: Cannot cast from source type to destination type. 3 Answers

Unable to cast from the source? 1 Answer

"Cannot cast from source type to destination type"- instantiating 4 Answers

InvalidCastException: Cannot cast from source type to destination type. 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges