Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

JavaScript to C#

Discussion in 'Scripting' started by wagglies, Aug 24, 2020.

  1. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    i have some code that is written in JavaScript however the version of unity i am using does not allow me to create a script that uses Java; i can only use C#. is there a way to have the code translated into C# or at least a way to input the JavaScript code in the C# document??

    the code is as follows:

    Code (JavaScript):
    1.  
    2. function OnTrigger(Col : Collider)
    3. {
    4.     if(Col.tag == "Player")
    5.     {
    6.         Application.LoadLevel("Level 2");
    7.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    If that's it, that's a trivial conversion. Just change the declaration to be:

    Code (csharp):
    1. void OnTrigger(  Collider col)
    and the rest remains same-same, except you need a closing brace.

    If you just copied the first few lines of a larger script, well, there are some semi-automated tools... just google for "unity javascript to c#" and see what's available.

    Also, you probably should use
    .CompareTag()
    rather than equality, but not a big deal.

    Also, note that none of the Unity collision events are called
    OnTrigger()
    . View the docs for their precise spelling and capitalization. Both are critical.
     
  3. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    so i tried that code and it didnt do what it was meant to haha. its probably the actual structure im trying to use

    anyways, what im trying to do is make it so that once the chest within the game is "picked up" it loads a "loading screen" that is timed and then loads the second level etc. would the code above be the way to do that or would you suggest something else?
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Well, as Kurt said above, OnTrigger is not a Unity callback, so Unity will never call that code for you automatically. You might be looking for this one: https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html.

    If you're ever wondering what the correct unity callback method names are, they're all listed here under "Messages": https://docs.unity3d.com/ScriptReference/MonoBehaviour.html
     
    Kurt-Dekker likes this.
  5. wagglies

    wagglies

    Joined:
    Jul 9, 2020
    Posts:
    9
    thank you so much. ive fixed it up so that it uses OnTriggerEnter and it works how it should for now (until i screw it up again lol)