- Home /
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.
You have it correct. Simply drag and drop in the editor. $$anonymous$$y question is, however, why would you want to do that? So what if script A is a bit larger. Is making it smaller worth this over complicated, heavy coupled, scheme you are trying to do?
Answer by Graham-Dunnett · Jul 16, 2013 at 01:53 PM
Use GetComponent()
to look-up ScriptB from ScriptA, and vice versa. See:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html
Answer by Graham-Dunnett · Jul 16, 2013 at 01:53 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110219im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Is this doable in unity 2 Answers
Difficulty Calling Variables from other scrtipts 1 Answer
How to write a script which will switch the scene after the character reached a location? 1 Answer
Using Enum across several files 3 Answers
How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers