- Home /
Access a script that has NO class?
I have a script I downloaded, and it works great, however it has NO class declaration!
I want to access it from another script, to change its variables, but I can't figure out how to use transform.GetComponent to access the script, because without a class delcaration, the script has no name? I tried GetComponent with its file name, but that didn't work.
The script is javascript (js). I'm calling it from a C# script. Hopefully that's allowed.
Answer by tanoshimi · Jan 04, 2015 at 09:07 AM
(Not quite sure how you know it "works great" when you haven't got it to work, but never mind)
UnityScript files, unlike C#, do not have explicit class declarations. You simply write Start(), Update() functions etc. in the body of the script and attach it to an object - it automagically creates a wrapping class that inherits from Monobehaviour.
To call from C#, place your .js script in the Standard Assets folder of your project to ensure it is compiled first, then simply attach to an object and call it using GetComponent as usual.
Thanks for the answer. I tried putting the file directly into the Assets/Standard Assets/ folder, so that it is now located at [project root]/Assets/Standard Assets/ParticleLines.js --- and then when I type transform.GetComponent, intellisence does not find it, I assume it isn't working. I also tried the same steps, but using the Plugins folder ins$$anonymous$$d, no luck there either. I'm trying to call it from a c# script that inherits from monobehavior. Any idea what I'm doing wrong?
Intellisense probably won't spot it. Use the force, Luke, and just type it anyway:
GetComponent<ParticleLines>().WhateverTheNameOfTheFunctionInThatClassIs();
Answer by Kiwasi · Jan 04, 2015 at 09:03 AM
With JavaScript the class name is the same as the file name.
Expect plenty of headaches working with the two languages. You generally have to put the JavaScript file into the plugins folder for this to work.