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 34638a · Dec 04, 2013 at 12:20 PM · raycastreading data

How to read data on a object that is hit by a raycast?

Hello people of the internet,

The general idea is simple: I have a camera that looks at my scene, from it a raycast goes out to the world, in the world are objects that have data. I want to read the data on those objects so I can place them on a GUI. if there a way to do this using java, if so please help!

Commenter: if the Raycast hits, it returns a RaycastHit. From the RaycastHit you can get Information about the hitted Object.

please don't take offence to this, person above, but that was very obvious I was wanting a script example if possible that would let me read the data in a script on the object

Regards - 34638a

Comment
Add comment · Show 6
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 felixpk · Dec 04, 2013 at 12:26 PM 0
Share

If the Raycast hits, it returns a RaycastHit. From the RaycastHit you can get Information about the hitted Object

avatar image ArkaneX · Dec 04, 2013 at 12:48 PM 0
Share

@34638a - if you want to reply, then please comment using 'comment' button. Additionally, Unity uses tailored version of JavaScript (a.k.a. UnityScript), which is not related to java.

avatar image 34638a · Dec 04, 2013 at 12:58 PM 0
Share

I did it like I did above because it was a point I did not address in the question above, also I had no idea that it was different I thought Unity used Java... guess not

avatar image ArkaneX · Dec 04, 2013 at 01:02 PM 0
Share

I see. In this case (I mean editing question), I suggest posting a separate comment anyway. At least "Question edited a bit", because users subscribed to this question will get a mail notification. Editing a question/answer/comment does not trigger such notification.

avatar image 34638a · Dec 05, 2013 at 11:16 AM 0
Share

ok thanks for the tips man I'm about to try the script you suggested, hope it works! :D

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ArkaneX · Dec 04, 2013 at 12:58 PM

If you want to read data on a script, then you can do:

 var script : YourScript = hit.transform.GetComponent(YourScript); // hit is RaycastHit, YourScript is the name of your script attached to the object
 var someNumber : int = script.SomeNumber;

Additionally, you can check hit.transform.tag before retrieving script, to determine if you hit proper object.

EDIT: to fill hit variable, you have to use Physics.Raycast(ray, out hit) - just like was explained in another answer and comment.

Comment
Add comment · Show 10 · 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 felixpk · Dec 05, 2013 at 11:24 AM 0
Share

I made very bad experiences with getting the tag from a transform. $$anonymous$$aybe i am not right but is it more stable to get the tag from the gameobject itself?

avatar image 34638a · Dec 05, 2013 at 11:44 AM 0
Share

I have 1 error: NullReferenceException: Object reference not set to an instance of an object c_RayCasting.Update () (at Assets/Scripts/core/c_RayCasting.js:20)

Script:

 #pragma strict
 
 public static var PR1 :int = 0;
 public static var PR2 :int = 0;
 public static var PR3 :int = 0;
 public static var PR4 :int = 0;
 public static var PR5 :int = 0;
 public static var PR6 :int = 0;
 public static var PR7 :int = 0;
 public static var PR8 :int = 0;
 public static var PR9 :int = 0;
 
 
 var HIT_RETURN : RaycastHit;
 
 function Update () {
     
     var ray : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);
     
     var script : CLONE_SCRIPT = HIT_RETURN.transform.GetComponent(CLONE_SCRIPT); // hit is RaycastHit, YourScript is the name of your script attached to the object
     PR1 = script.LOCALVALUES[0].Stat1;
     
     Debug.DrawRay(ray.origin, ray.direction * HIT_RETURN.distance, Color.cyan);
     
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         
         Debug.Log(PR1);
         
         
 //        Debug.Log("Stats are: ");
 //        Debug.Log("");
         
     }
     
 }

script its's linked to:

 #pragma strict
 
 public var LOCALVALUES : IV_VC[];
 
 var Creature_ID : int;
 
 function Start () {
 
     Creature_ID = IV_call.ID;
     
     Debug.Log(Creature_ID);
     
         LOCALVALUES[0].Stat1 = IV_call.Stat1H;
         LOCALVALUES[0].Stat2 = IV_call.Stat2H;
         LOCALVALUES[0].Stat3 = IV_call.Stat3H;
         LOCALVALUES[0].Stat4 = IV_call.Stat4H;    
         LOCALVALUES[0].Stat5 = IV_call.Stat5H;
         LOCALVALUES[0].Stat6 = IV_call.Stat6H;
         LOCALVALUES[0].Stat7 = IV_call.Stat7H;
         LOCALVALUES[0].Stat9 = IV_call.Stat9H;
                 
     
     //DEBUG 
     /*
         Debug.Log(LOCALVALUES[0].Stat1);
         Debug.Log(LOCALVALUES[0].Stat2);
         Debug.Log(LOCALVALUES[0].Stat3);
         Debug.Log(LOCALVALUES[0].Stat4);
         Debug.Log(LOCALVALUES[0].Stat5);
         Debug.Log(LOCALVALUES[0].Stat6);
         Debug.Log(LOCALVALUES[0].Stat7);
         Debug.Log(LOCALVALUES[0].Stat9);
     */
     
 }
 
 function Update () {
 
 }

IV_VC is just a inventory holder type script.

avatar image Hoeloe · Dec 05, 2013 at 11:53 AM 0
Share

I seriously recommend using Arrays ins$$anonymous$$d of a number of distinct variables of the same type. Why use PR1-9, ins$$anonymous$$d of an array called PR of length 9? It would make the code much neater, and easier to manage. The only reason you'd use fields ins$$anonymous$$d of arrays is to get more descriptive names out of it, but these are just numbered fields - numbered fields of the same type should ALWAYS go in an array or similar structure, NEVER as separate variables.

As for your error, it's because you're never assigning anything to HIT_RETURN. What do you expect it to do? $$anonymous$$agically fill in the information because you defined a ray?

avatar image ArkaneX · Dec 05, 2013 at 12:07 PM 0
Share

@34638a - you see, when you wrote that using RaycastHit is obvious, I thought that you know how to use it, and I posted only a script related to data retrieval...

Answer updated.

avatar image 34638a · Dec 05, 2013 at 11:43 PM 0
Share

yes person that says ALWAYS USE ARRAYS!!!!! I do know that but to reference the way I have because it links to enum's of different values not just int's or strings its simpler to list like I have. thanks thanks for the CAPS FOR E$$anonymous$$PHASIS it really provides great moral support :P but still any advice is helpful :D

also that is a outdated script that was before I made a edit to render it with GUI input so the variables are not all int's any more they were place holders at the time.

Show more comments
avatar image
0

Answer by felixpk · Dec 04, 2013 at 02:26 PM

             RaycastHit hit;
             Ray ray;
             GameObject gameObj;
             
             if(Physics.Raycast(ray,out hit,Mathf.Infinity){
                 gameObj = hit.collider.gameObject;
             }

I hope that helps, I think it is easy to understand ..

Comment
Add comment · Show 3 · 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 sdgd · Dec 05, 2013 at 12:03 PM 0
Share

no it's not easy to understand this is something completely un ordinary especially "out - hit" it gave me headaches at the beginning.

besides you don't need $$anonymous$$athf.Infinity

out is something like ref

Physics.Raycast(/.../) is a Boolean function in this function refered hit is changed. so what ever it was before it will no longer be that.

 RaycastHit hit;
 Ray ray;
 GameObject gameObj;
 
 // Boolean function witch refers RaycastHit changes it in function
 // and returnes if anything was hit in ray direction.
 // else iirc hit becomes null
 if(Physics.Raycast(ray,out hit){
     // hit contains colliders distance and Vector3 point where it hit it.
     gameObj = hit.collider.gameObject;
 }

BTW I wouldn't comment on this if you wouldn't say: I hope that helps, I think it is easy to understand ..

avatar image felixpk · Dec 05, 2013 at 12:08 PM 0
Share

So what is the Problem? In 2 hours i am at my pc, i will give you a fully working sollution, can't understand why you are mad now, i just want to help

avatar image 34638a · Dec 05, 2013 at 11:38 PM 0
Share

I have fixed the problem I just made a small mistake in the call reference for the object being hit :P

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[Solved] Reading same data from multiple Scripts 2 Answers

Raycast only works once 1 Answer

RaycastHit returns object without collider, in wrong layer 1 Answer

Problem With RayCast 3 Answers

Problem with raycast 2 Answers


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