- Home /
Is there any way to reference the object that a raycast hits
Basically, i have a reticle that shows where the player is looking. if the player sees the object, i want to change the material color. Originally, i had this setup in the script whose material is changing, the problem was that all of the other materials colors were changing as well (i have multiple objects the player can look at that all have the same script attached). So i tried setting this up in the script for the reticle. However, it seems that i am not able to actually reference propertys of the other object through a raycast? Heres my code
using UnityEngine;
using System.Collections;
public class reticleScript : MonoBehaviour {
public Camera cameraFacing;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.LookAt (cameraFacing.transform.position);
transform.Rotate (0.0f, 180.0f, 0.0f);
RaycastHit hit;
if (Physics.Raycast (transform.position, Vector3.forward, 10, out hit)) {
Orbit.isSelected = true;
/*****This is where im having trobule*/
hit.collider.gameObject.mat = Resources.Load("mat_Orange",
typeof(Material)) as Material;
gameObject.GetComponent<Renderer>().material = mat;
}
Is what im trying to do impossible? if so, is there a better way i should be going about this?
hit.collider.gameObject
its not just referencing the gameObject, i need to refence that gameObjects properties
$$anonymous$$aterial new$$anonymous$$aterial = Resources.Load("mat_Orange", typeof($$anonymous$$aterial)) as $$anonymous$$aterial;
// this is the material for the object that is hit by the raycast.
// if you change this material to another it should be ok
hit.collider.gameObject.GetComponent<Renderer>().material = new$$anonymous$$aterial;
Raycasting is one of the trickier things to do. $$anonymous$$eep reading the manual and examples. It sounds like some of your trouble s just because you haven't worked with Unity that long.
("i need to refence that gameObjects properties"?? If you have the gameObject, no matter how you got it, you can always mess with all the parts in it. That's a big Unity/C# trick.)
im not new to unity just never really had to use raycasting before.
Answer by AaronC · Jun 23, 2015 at 02:58 AM
Found an old post
Once you have determined that your raycast actually hit something, you can query your 'hit' object for many different properties.
For the gameObject's name:
hit.collider.gameObject.name;
For the object's tag:
hit.collider.tag
And there are many others - see the RaycastHit manual page.
Because "collider" is a component, you can use "GetComponent" on the hit.collider result, to get access to any other component on the gameObject - including your own script.
You would use something like this (inside your , assuming your script is called "BoomScript" and it has a function in it called "Boom":
if (Physics.Raycast (ray, hit, 1000)) {
var boomScript : BoomScript = hit.collider.GetComponent(BoomScript);
if (boomScript != null) {
boomScript.Boom();
}
}
Your answer
Follow this Question
Related Questions
Difference between hit.collider.gameObject vs hit.transform.gameObject 2 Answers
raycast not colliding 1 Answer
Checking raycast hit on tagged collider- "Cannot implicitly convert type 'string' to 'bool'" 1 Answer
RaycastHit2D hits "itself" 3 Answers
How to make the player jump down from the platform 0 Answers