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
0
Question by MagicN · May 21, 2014 at 01:05 AM · loadlevelgetkeydown

(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;
 }
 }
Comment
Add comment · Show 5
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 doggan · May 21, 2014 at 01:23 AM 0
Share

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

avatar image Maerig · May 21, 2014 at 01:25 AM 0
Share
  • Check that your Player has a RigidBody attached

  • $$anonymous$$ake sure that both colliders have "Is Trigger" checked

avatar image MagicN · May 21, 2014 at 01:26 AM 0
Share

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.

avatar image Maerig · May 21, 2014 at 01:39 AM 0
Share

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.

avatar image MagicN · May 21, 2014 at 01:55 AM 0
Share

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.

1 Reply

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

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.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When I go inside a collider I want GUI Text to appear, and allow me to press 'e' to swap scenes, but it instantly transports me to the scene. 1 Answer

[Solved] Menus advancing/returning several at a time 0 Answers

Door script not loading the next scene? 1 Answer

Ipad's load indicator for loading scenes. 1 Answer

Im trying to make a Java script that detects how many tags of Enemy are still alive. How exactly do i do that? 2 Answers


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