- Home /
how to trigger another script in unityscript
hi i was wondering is there a way to trigger a separate script in a script???? E.G. the script to make a gun fire and the script to make the bullet move, thats just an example
Answer by Berenger · Mar 07, 2012 at 07:52 PM
All scripts are components. You can access components by a reference. Once you can access it, you can call any public function it has. To access that reference, either use a public var and the inspector, or use Find functions. Among other ways.
A question on top of that, do you need to explicitly declare a public var or is it done by default? I have been trying to access a variable of a script using var some : scriptName = GetComponent(Scriptname); but it would not find the variable. Using static, I can reach it but I don't want to do that.
I'm used to C#, where variables are private by default, when they are public in javascript. If Scriptname have a var declared "var plop : int;" you can access it with scriptName = GetComponent(Scriptname) and scriptName.plop. Only if the component exist.
You also can't declare a variable private that you are going to use with a GetComponent. It has to be public as such. If you don't want that variable to show up in the inspector but you want to use it in a GetComponent reference, use @System.NonSerialized.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064832im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do Ensure that a series of int's are never the same? 1 Answer
How to restrict a button for a second 1 Answer
Semicolon error using JS 1 Answer