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

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

@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.

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

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.

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

2 Answers

2

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.

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

@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.

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 EMPHASIS 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.

I'm not sure why it makes a difference that it's referring to enums of different values if you're storing them all as ints - keep them in an array anyway. If you want to refer to them as enums, then make them the type of the enum you want to refer them to.

its because I'd rather not jump through 20 more hoops to convert the ID value of the enum to a String that stores the name of the enum itself and than another one that has the name of the value to set the enum to when I'm just reading it from the object than render that new string on a GUI.

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 …

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

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