- Home /
Editing JScript Varaibles in C#
I have 2 scripts that need to communicate to each other.
The first script is a JScript and it controls a plane's texture, speed etc. I need to modify the texture varable and then run a function with a script that is C#.
Is it possible to do what i require and if so, how do i do it?
Answer by LPGaming-Company · Jan 18, 2013 at 03:45 AM
C# is by default compiled before JavaScript, the easy workaround which I use is to drop the JavaScript into the StandardAssets folder, then in your C# script call this.
ScriptName script;
then in Start() { make sure to set it
script = GetComponent();
You would call the variables or change them by doing the following
script.varaible = 10; script.variable += 10; if(script.variable == 10)
etc
Exmaple:
public class Example : MonoBehaviour {
ScriptName script;
void Start() {
script = GetComponent<ScriptName>();
}
void Update() {
if(script.health == 0)
ApplyDead();
}
}
I ended up looking around on the forums and found something very similar to this, and it works a treat! thank you very much.
No problem, I know the pain. Just the other day I ended up writing my $$anonymous$$ovement handler in Java ins$$anonymous$$d of C# because in C# you have to create temporary values for everything which gets really annoying and messy. Ran into the same exact problem. Some things are just better off in JavaScript. I managed to create my own folder in the StandardAssets called "CustomScripts" so that I know where all my .js files are.
Also, if the answer helped, $$anonymous$$d giving it a thumbs up aswell? I get attacked for not doing so.
Answer by Eric5h5 · Jan 18, 2013 at 01:38 AM
It's possible, but the Unityscript file must be earlier in the compile order. See here, particularly section 3. Two-way communication is not possible, however. In general it's better to avoid mixing languages in this way (it's fine when you have static library functions that don't need to access other scripts).
Answer by jmgek · Jan 18, 2013 at 01:27 AM
Its better to use C# because it does not waste resources trying to guess what 'var's you use. But if you still need to use .js you should convert everything to java.
There are converters out there. here is a C# to java. http://files.m2h.nl//js_to_c.php
Dont need to get buthurt that I dont spell out javascript. and no its not factually wrong. In order for unity to compile java it needs to look up its own variables and assign them thats why you dont need to state what they are. C# is faster.
Yes it is 100% factually wrong. Just like C#, Unityscript uses type inference to assign types to variables, which is done at compile time and has no effect on speed. For example, "var foo = 5;" is valid C# and Unityscript code, and statically types foo as an int in both cases. It's exactly the same as using "var foo : int = 5" in Unityscript or "int foo = 5" in C#. But don't take my word for it, use the profiler or run some benchmarks and you'll find that C# and Unityscript in general have essentially identical speed.
Java and Javascript are two entirely different languages, so it's necessary to spell it out. (Or use "Unityscript" ideally, since that's more accurate anyway.)
First of all if you cant understand javascript when I say java IN A UNITY FORU$$anonymous$$. you are either stupid or a egotistical prick who needs to calm down.
Secondly C# can potentially yield in higher optimization possibilities cause it does not hide away what it really has to do and so you are not left in the dark when something isn't working the way you would like it to. also because you state your variables it dose not have to look through its own code.
It makes you sound quite ignorant if you write "Java" when you mean "Javascript". They are different things whether you like it or not; it's that simple (and it is valid to talk about Java in a Unity forum, in the context of Unity Android). It's pretty clear you don't really know what you're talking about here, so I'll just leave it at that, since you're obviously not inclined to be rational. I'm sorry you're one of those people who can't be corrected without getting angry and defensive about it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613091128im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object to JSON Conversion 2 Answers
How should i draw a bezier curve in a compute shader? (Already works in c#) 0 Answers
Publishing C# game in Android 1 Answer