How to use multiple scripts

Hi, I’ve been googling for over an hour but I can’t really seem to find the correct way to do this.

In short:

  • I got an objectA with scriptA attached
  • I want to shorten scriptA by making functions and calling them from scriptB with key presses
  • 1 function f.e. needs to start a timer

Right now it’s working by making an objectB with scriptB attached, make a variable in ObjectA and drag ObjectB in it and vice versa.

ObjectA

 var scriptB : ScriptB;
 var startTime;
    function Update ()
    {
     scriptB .StartTimer();
    }

Object B

var scriptA : ScriptA;

function StartTimer(){
 scriptA .startTime = Time.time;
}

But is this the way to do it?
I’m thinking to just drag scriptA and scriptB on objectA, but how would I call functions and pass parameters from scriptB to scriptA.

I know gameobject.find(“”) exists, but I read it’s to be avoided.

Use GetComponent() to look-up ScriptB from ScriptA, and vice versa. See:

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

Use GetComponent() to look-up ScriptB from ScriptA, and vice versa. See:

http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

Check the example out at the bottom of this page. Ideally you’d use GetComponent() in your Start function.