x


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.

more ▼

asked Oct 26 '10 at 03:17 AM

Storm Kiernan gravatar image

Storm Kiernan
177 18 25 32

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 01 '10 at 07:37 PM

lhk gravatar image

lhk
520 14 15 24

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 01 '10 at 07:37 PM

skovacs1 gravatar image

skovacs1
10k 11 25 91

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x161
x83
x35

asked: Oct 26 '10 at 03:17 AM

Seen: 1368 times

Last Updated: Nov 01 '10 at 07:25 PM