- Home /
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
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.
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?
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.
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?
@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 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.
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 ..
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 ..
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
Your answer
Follow this Question
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