- Home /
(Resolved) Load a Level by pressing a Key
This is my very first script, viewing and learning scripting along the way. I'm trying to have the player load the next level by triggering a box collider. This is what I have so far...not sure what I'm doing wrong. Sorry for being a noob.
#pragma strict
function Update (){
if(Input.GetKeyDown("e") && enter)
{
Application.LoadLevel(2);
}
}
function OnGUI(){
if (enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100,150,30), "Press E to turn on TV");
}
}
function OnTriggerEnter (other : Collider)
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider)
if (other.gameObject.tag == "Player") {
enter = false;
}
}
What is the actual problem you are having? $$anonymous$$any things could be wrong.
If it is entering properly into the if statement
within your Update function, then it might be that you haven't added your scenes to the scene list yet (File -> Build Settings): http://docs.unity3d.com/Documentation/ScriptReference/Application.LoadLevel.html
Check that your Player has a
RigidBody
attached$$anonymous$$ake sure that both colliders have "Is Trigger" checked
I apologize for not specifying. I have my Build Settings all set up. This is what its telling me
Assets/Standard Assets/Scripting/Load Level.js(17,1): BCE0043: Unexpected token: if.
If your script is as you posted it, you're missing a brace at the beginning of two of your functions. It should be
function OnTriggerEnter (other : Collider) {
and
function OnTriggerExit (other : Collider) {
If you haven't done it somewhere else in your script already, you need to declare the enter
variable, too.
That was it doggan, thanks a bunch. Just like $$anonymous$$aerig said, I didnt declare the "enter" variable.
I'm still learning, you guys are epic.
Answer by doggan · May 21, 2014 at 01:44 AM
Here's how it should look:
#pragma strict
var enter : boolean = false;
function Update (){
if(Input.GetKeyDown("e") && enter)
{
Application.LoadLevel(2);
}
}
function OnGUI(){
if (enter){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100,150,30), "Press E to turn on TV");
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.tag == "Player") {
enter = false;
}
}
In general, if you get a compile error, you should look at the previous line to make sure nothing is wrong. So in your example, the console error was appearing on line 17, but the actual bracket error was on line 16.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145518im_/https://answers.unity.com/themes/thub/images/avi.jpg)