Question by
SamCarey · Apr 23, 2016 at 12:39 AM ·
gameobjectraycasttransformvariable
Add GameObject Raycast hits to a variable
I'm trying to make a variable within my raycast script that store the GameObject you are locking at. This is my script
var HitStore : GameObject;
function Update ()
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
if (Physics.Raycast (ray, hit, 60))
{
if (hit.collider.gameObject.tag == "WaterStore")
{
HitStore = hit.gameObject;
if (Input.GetKeyDown(KeyCode.E))
{
hit.transform.SendMessage("MoveWater", SendMessageOptions.DontRequireReceiver);
}
}
}
Comment
Best Answer
Answer by KidRigger · Apr 23, 2016 at 01:11 AM
hit is a RayCastHit but RayCastHits don't contain a gameObject variable in it. However it contains transform which in turn contains GameObject
HitStore = hit.transform.gameObject
should work.
Your answer
Follow this Question
Related Questions
Enemy Attacking Even When Not In Range 2 Answers
Variable not being assigned 0 Answers
When I Raycast Hit Object set to fixed hand position. 1 Answer
Multiple game objects same script 0 Answers
How would I project a Gameobject onto the surface of another? 0 Answers