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 Ppa0 · Oct 12, 2011 at 04:39 AM · c#raycastmouserayselect

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;
 }
 
 }
 }
 }



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

3 Replies

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

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

Comment
Add comment · Show 2 · 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 aldonaletto · Oct 12, 2011 at 08:32 PM 0
Share

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.

avatar image Ppa0 · Oct 15, 2011 at 06:34 PM 0
Share

thank you very much

avatar image
0

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;
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
avatar image
0

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.

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 jorg van immerseel · May 24, 2014 at 08:57 AM 0
Share

can this script be used to make something appear on the place you clicked in s$$anonymous$$d of giving a number?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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