x


C# mouse Raycast question

I know there are a lot of mouse raycast questions, but most of them are in javascript and even when I find some answers in C# I still can't seem to solve my problem. So here it is:

I am trying to click a 1 of 4 boxes with my mouse, all 4 boxes are named differently and have different tags. They all have a box collider, and what is suppose to happen is when I select one of them, it shows me which one is selected via an integer number 1-4. So here is the code:

public class Selection : MonoBehaviour
{

public RaycastHit hit = new RaycastHit();
public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
public int number = 0;

void Update ()
{
selection();
}

void selection()
{
if(Input.GetKey(KeyCode.Mouse0))
{
if(Physics.Raycast(ray,hit,100)) // < this is the problem
{
if(hit.transform.tag == "c1")
{
number = 1;
}
if(hit.transform.tag == "c2")
{
number = 2;
}
if(hit.transform.tag == "c3")
{
number = 3;
}
if(hit.transform.tag == "c4")
{
number = 4;
}

}
}
}
more ▼

asked Oct 12 '11 at 04:39 AM

Ppa0 gravatar image

Ppa0
16 8 8 10

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

2 answers: sort voted first

When I do as you say, I don't get any errors initially but as soon as I hit play, the raycast doesn't work, and I get like 20-25 errors saying things like:

ArgumentException: you are not allowed to call INTERNAL_CALL_ScreenPointToRay when declaring a variable

UnityEngine.Camera:get_main()

those 2 errors repeat pretty much, there are few small differences between them all but still I don't understand why that is. I also want to point out that this script is attached to the main camera, I don't know if that is an issue or not

more ▼

answered Oct 12 '11 at 07:11 AM

Ppa0 gravatar image

Ppa0
16 8 8 10

Unity is complaining because you are declaring a public variable and assigning a property or function result to it in the same instruction:

public Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

You should only declare the variable, and assign the ScreenPointToRay in another instruction - in your case, it should be done inside Selection, since the ray must be based on the current mouse position:

public Ray ray; // just declare the variable
...
function Selection(){
    // assign the ray created to the variable here 
    ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    if (Physics.Raycast(...

NOTE: Please only use the Your answer box to answer the question - use the minuscule add new comment button in the answer or question to post comments or replies.

Oct 12 '11 at 08:32 PM aldonaletto

thank you very much

Oct 15 '11 at 06:34 PM Ppa0
(comments are locked)
10|3000 characters needed characters left

I'm not a C# expert, but it seems that you must write:

if(Physics.Raycast(ray, out hit, 100))

And the declaration of the RaycastHit variable should be just:

RaycastHit hit;
more ▼

answered Oct 12 '11 at 04:47 AM

aldonaletto gravatar image

aldonaletto
42.5k 16 42 202

(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:

x4370
x1578
x1001
x155
x91

asked: Oct 12 '11 at 04:39 AM

Seen: 4316 times

Last Updated: Oct 15 '11 at 06:34 PM