- Home /
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;
}
}
}
}
Answer by Ppa0 · Oct 12, 2011 at 07:11 AM
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
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 $$anonymous$$uscule add new comment button in the answer or question to post comments or replies.
Answer by aldonaletto · Oct 12, 2011 at 04:47 AM
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;
Answer by CJokes · Jul 20, 2013 at 06:41 PM
Firstly, as mentioned by aldonaletto, your missing the "out" keyword in the second paramater of Physics.Raycast():
if(Physics.Raycast(ray, out hit))
Also, FYI, you don't need to have the third parameter unless your limiting the length of the Ray.
Secondly, if you want Selection() to work for multiple clicks you need to reset ray within Selection() each time its called. The code above will only work for a single click and will keep printing out the same number regardless of following clicks because ray is never updated with the current mouse position:
void selection() {
if(Input.GetKeyDown(KeyCode.Mouse0)) {
// Set or reset ray here
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)) {
The following is the full script I used with functionality to draw a red line to the selected object in the Scene window for selection clarification:
using UnityEngine;
using System.Collections;
public class Selection : MonoBehaviour {
Ray ray;
RaycastHit hit;
public int number = 0;
void Start () {
// Initialise ray
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Print out the current number value to the console window
Debug.Log("Number is currently: " + number);
}
void Update () {
Selection();
}
void Selection() {
// Use Input.GetKeyDown() for single clicks
if(Input.GetKeyDown(KeyCode.Mouse0))
{
// Reset ray with new mouse position
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit)) {
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;
}
// Reset number to zero if no object selected
if (hit.transform.tag == "Untagged") {
number = 0;
}
Debug.Log("Number is currently: " + number);
}
}
// Draw a red line from camera to selected object in Scene window
Debug.DrawLine(ray.origin, hit.point, Color.red);
}
}
Hope this helps.
can this script be used to make something appear on the place you clicked in s$$anonymous$$d of giving a number?
Your answer
Follow this Question
Related Questions
how do i fix this small error 2 Answers
Translate Mouse to Worldspace 0 Answers
Mouseclick GameObject through the view of a RenderTexture 0 Answers
click on a game object that script isn't attached to 1 Answer
For each Raycast? 1 Answer