How to acces rayhit location from a different script
Hello fellow game developers.
I am just wondering if anyone can help me out.I have a script A which is on my main camera and it's creating rays where ever i click.I was wonder how would i acces the location of that ray location which hits a plane mesh from a script B?
Answer by gameplay4all · Mar 01, 2017 at 01:58 PM
If multiple scripts need to use that value throughout the game, you can make the value static. So you'd have something like this:
public class CameraRay : MonoBehaviour {
public static Vector3 hitPosition;
void Update() {
//....stuff....
hitPosition = Hit.point;
}
}
Now every object in the scene has access to the location by: CameraRay.hitPosition
Hope this helps!
-Gameplay4all
Answer by SolarStorm · Mar 01, 2017 at 02:12 PM
So far i just have one script that would need to acces the location of that ray.What can i do if the value changes? Since the ray changes the location beacause it depends on where you click on the screen.
Thanks for the help :)
Everytime you calculate the hit point, in my example that was in Update(), you assign the new position to the static variable. This way it keeps updated and CameraRay.hitPosition is always the correct position. So eacht time you do your raycast, set hitPosition to the point of the hit :)
Sweet i'll try it and post when i try.Really apreciated the help :) thanks again
Your answer
![](https://koobas.hobune.stream/wayback/20220612112220im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Raycast not taking full length 0 Answers
Scripting Errors 1 Answer
Raycast not detected on instantiated object 0 Answers
Shoot towards mouse except mouse y 1 Answer
rigidbody,addforce is affecting all clones which are clicked on through raycast 0 Answers