- Home /
SendMessage and RaycastHit
I am setting up a selection system like one you might see in an RTS game. Having said that, I have a slection script to be placed on every selectable prefab/gameobject. Inside this script i have a function SetSelect().
What I am trying to do is raycast to the object, then call that object's SetSelect function.
Shouldn't I be able to say:
GameObject myObj = rayCastHitInfo.collider.gameObject;
myObj.SendMessage("SetSelect", SendMessageOptions.DontRequireReceiver);
I would THINK so. Maybe I'm thinking all wrong. Anyway it doesn't call the function. I'm not sure why.
Answer by lhk · Nov 01, 2010 at 07:37 PM
Before you can access the raycasthitinfo object you need to actually cast a ray. Your script needs some more lines
RaycastHit rayCastHitInfo;
if(Physics.Raycast(origin,direction,rayCastHitInfo))
{
//here comes your code
}
Read the scripting reference for futher information.
Answer by skovacs1 · Nov 01, 2010 at 07:37 PM
It may have to do with when or how you are getting your GameObject or when or where you are calling the functions or even just how you're testing whether the function is being called. Without more information like a more complete section of script, it is impossible to guess.
I just tested this setup and it works:
CameraPicker.js
var hit : RaycastHit;
function Update() { if(Input.GetMouseButtonUp(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit)) { hit.collider.gameObject.SendMessage("Test"); } }
Selectable.js
function Test () {
Debug.Log(gameObject.name);
}
If this does not address your issue, please include more information as it is likely some other part of your script that is not behaving as you expect.
Your answer
Follow this Question
Related Questions
SendMessage , how can i send two parameter 3 Answers
Send Message Has no Receiver 1 Answer
How to Send Message to other GameObject 3 Answers
BroadcastMessage 1 Answer