Using a raycast across multiple scripts

Hi guys,

I was just wondering if it’s possible to use a raycast to detect if an object is in front of the player, then if there is an object there, increase an integer on a seperate script.

So for instance,

We have a gameobject with a collider called fire_place

We have an integer on the fire_place script called wood

We have a raycast coming from the player

The raycast hits the fire_place, and the wood integer increases.

Is that possible? If so can someone point me in the right direction to figure this out? Thanks.

1 Answer

1

var ray = Camera.main.ScreenPointToRay();
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) {
var fp = hit.collider.GetComponent(fire_place);
if (fp != null) {
fp.wood++;
}
}

http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://unitygems.com/script-interaction1/

http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/