can't use string in function to load scene? Javascript

I create code and then attach it to a button within the OnClick() in the inspector which works for c sharp

but it doesn’t work for Javascript?

#pragma strict

function LoadScene (String level)

{
Application.LoadLevel(level);
}

Here is the C Sharp script which works…

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayButton : MonoBehaviour {

    public void changemenu(string scenename)
    {
        Application.LoadLevel(scenename);
       

    }
}

so basically I want to create a string for different scenes and then use it on the OnClick section in the inspector

the code works fine for c sharp

I apply the same logic to Javascript but it doesn’t work, what am I doing wrong?

When passing variables into Javascript, they must be declared like this

function LoadScene (level : String)
{
    Application.LoadLevel(level);
}

the syntax of declaration is completely different to C#. your problem was because it doesn’t accept that input you should of had an error appear in your console log stating so.

Also, if you know the C# solution why try convert to JavaScript? there’s a lot more help online for C# than their is Javascript

hey i figured it out before i read your message

i forgot the ‘:’

#pragma strict
import UnityEngine.SceneManagement;

var level : String;

function LoadScene (level : String)
{

SceneManager.LoadScene(level);
}

and I have a test tomorrow and they want Javascript sadly or C Sharp would be way better

nonetheless thanks so much :slight_smile:

:open_mouth: They want you to use JavaScript in Unity? Well I’m surprised haha

Both Application.LoadLevel and Unity’s implementation of JS are deprecated. JS will have a soft removal in 2017.2 (next major release, can still compile JS but you won’t be able to create new JS scripts in the Unity UI). Application.LoadLevel will be removed in some future build, and was replaced by SceneManager.LoadScene back in 5.3 or so.

I’d suggest not starting any new projects using either.