c# editing gameobject returned by a raycast

Im fairly new to scripting and Im still learning the basics. I have a basic first person character controller, and some game objects in my scene. So far what i can do its run a round and cast a ray from my character. id like to be able to modify the object that the ray returns.

say if the tree i clicked on had a script with a “fallDown” function or somthing, is there a way i can call that from the ‘hit’ variable that my raycast returned into?

using UnityEngine;
using System.Collections;

public class AimScript : MonoBehaviour {[[
	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () 
		var ray = new Ray (transform.position, transform.forward);
		Debug.DrawRay (ray.origin, ray.direction * 10, Color.red);//test
		RaycastHit hit;
		if(Input.GetButtonDown ("Fire1") && hit.distance <= 6)
			// somthing like:
			// if( hit has TreeScript component )
			// run TreeScript's falldown function.

any help would be much appreciated.

Your easiest method is:

  hit.collider.SendMessage("DoSomeFunction", anOptionalParameter, SendMessageOptions.DontRequireReceiver);


  hit.collider.SendMessage("DoSomeFunction", SendMessageOptions.DontRequireReceiver);

Which will call DoSomeFunction on any script attached to the object that has one. You can also BroadcastMessage to include children or SendMessageUpwards to include parents.

You can look for a specific script using GetComponent

      var someScript = hit.collider.GetComponent<SomeScript>();
      if(someScript != null) 
          someScript.DoSomeFunction(with, some, parameters);