- Home /
Script Structure Problem
I found a script that goes like this;
//Script to revert winning player back to main menu
var levelToLoad : String;
function OnTriggerEnter(hit : Collider) { Application.LoadLevel(levelToLoad);
}//END FUNCTION ONTRIGGERENTER
But when I copied and pasted into new script, it tells me to fix compiler errors. How should I structure this script? Please help I am new to coding.
What does the error tell you? I would assume that if you just use it like that so it would say something about the "levelToLoad" because it is not defined on that piece of code.
it doesn't say what the error is, it just tells me to fix it.
Answer by Calum1015 · Apr 27, 2015 at 01:43 AM
var levelToLoad : String;
function OnTriggerEnter(hit : Collider)
{
Application.LoadLevel(levelToLoad);
}
I believe that is what you need.
Usually the errors are not caused by the layout of the code..or am I just missing the modifications in syntax. Without testing the code I would say that just by dropping the Application.LoadLevel down a line won't solve the problem. I would assume that the problem is that the code tries to load a level defined bu levelToLoad but it's not defined...you need to load a scene. For example I used Application.LoadLevel ("$$anonymous$$urhapeli"); in my project. The "$$anonymous$$urhapeli" is the name of the scene that I want to load. That little piece of code tries to load a scene that is defined by the var levelToLoad but in that little piece it is not defined with the scene name...So without knowing anything else about the code I would say that it is the problem
Oh and also, you define levelToLoad in the inspector. As long as you add the scene to the build settings it should work.
But still I don't think the commands need to be on new lines :D 'cos I usually have this one liner when I load from main menu
public void StartButton(){Application.LoadLevel ("Playground");}
Just need to change the name and it needs to be included to build settings. or does JS require them to be on new lines?
Your answer
![](https://koobas.hobune.stream/wayback/20220613194107im_/https://answers.unity.com/themes/thub/images/avi.jpg)