Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Lachee1 · Jan 18, 2013 at 01:10 AM · c#javascriptconversion

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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();
      }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lachee1 · Jan 19, 2013 at 02:18 AM 0
Share

I ended up looking around on the forums and found something very similar to this, and it works a treat! thank you very much.

avatar image LPGaming-Company · Jan 19, 2013 at 02:23 AM 1
Share

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.

avatar image LPGaming-Company · Jan 19, 2013 at 02:23 AM 0
Share

Also, if the answer helped, $$anonymous$$d giving it a thumbs up aswell? I get attacked for not doing so.

avatar image
1

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Jan 18, 2013 at 01:32 AM 0
Share

This is factually wrong. Also, Unity does not use Java.

avatar image jmgek · Jan 18, 2013 at 02:06 AM 0
Share

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.

avatar image Eric5h5 · Jan 18, 2013 at 02:13 AM 1
Share

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.)

avatar image jmgek · Jan 18, 2013 at 03:09 AM 0
Share

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.

avatar image Eric5h5 · Jan 18, 2013 at 03:27 AM 2
Share

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.

avatar image
0

Answer by DaveA · Jan 18, 2013 at 02:23 AM

Try using SendMessage

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges