Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by vevebe · Nov 23, 2015 at 05:14 PM · unity 5javascript

I'm very new to scripting and here is my question: what is wrong with my script? no errors, I want to press a key (f) in my trigger to load a new level (my trigger is next to a door)

function OnTriggerEnter(){

 if(Input.GetKey ("f"))
 {
     Application.LoadLevel("firsthouse");
 }


}

Comment
Add comment · Show 3
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 McGravity · Nov 23, 2015 at 06:03 PM 0
Share

Please read this carefully: http://docs.unity3d.com/ScriptReference/Input.Get$$anonymous$$ey.html

avatar image fredz0004 · Nov 24, 2015 at 01:44 AM 0
Share

Why is this on OnTriggerEnter? OnTriggerEnter is for detecting collisions with gameObjects that have colliders attached to it. I would try putting this on Update, just as the example on the reference that @$$anonymous$$cGravity linked. http://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

avatar image Le-Pampelmuse · Nov 24, 2015 at 08:03 AM 1
Share

Hi, first things first:

  • Please use a short, descriptive text for the questions title. Not the whole question.

  • This means logically, please describe your question in the questions text ;)

  • Please make sure your code is formatted properly. The first line is not formatted as code for example.

Here a re$$anonymous$$der: Frequently Asked Questions and the User Guide.

@itsharshdeep gave a accurate answer that solves what ever problem you might have. ;)

Your main problem as using OnTriggerEnter(), which is only called once everytime something enters the trigger.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by itsharshdeep · Nov 24, 2015 at 04:11 AM

Hello,

I don't know what you trying to achieve but as fas as I know there are two possibility you wanna achieve 1) When you will press "F" Buttons the scene should load.

 void Update(){
     if (Input.GetKeyDown (KeyCode.F)) {
         Application.LoadLevel("SceneNameHere");
           }
 }

 void OnTiggerEnter(){
 // Do What you wanna do here :)
 }
 

2) Now If you don't want the above thing but you need that when the OnTriggerEnter() should call then load the scene

 void OnTiggerEnter(){
  Application.LoadLevel("SceneNameHere");
 }


*Note:- The only mistake in your code is, It will only execute only just when OnTriggerEnter() is called and the same time you press "F" button.

I'm also assuming that you want to do like, there is a region present in the scene and if the player is in that region & if s/he press the "F" button then the scene should load. If that is the case then you have to do the following :-

   void OnTriggerStay(Collider coll){
         if ( coll.transform.tag == "Player" && Input.GetKeyDown(KeyCode.F)){
             Application.LoadLevel("SceneNameHere");
         }
     }
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
avatar image
0

Answer by shane_123 · Nov 24, 2015 at 11:06 AM

This is in C# but Don't forget to go in to file, Build Settings... And add your levels

     void Update () {
     if(Input.GetKey(KeyCode.F))
     {
     Application.LoadLevel("firsthouse");
     }
     }


Comment
Add comment · Show 4 · 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
avatar image Le-Pampelmuse · Nov 24, 2015 at 11:08 AM 0
Share

When posting answers containin code, please make sure your code contains no errors.

 If(Input.Get$$anonymous$$ey($$anonymous$$eyCode.F))

throws a parsing error because keywords are case-sensitive.

avatar image shane_123 · Nov 24, 2015 at 02:45 PM 0
Share

Try to add the script to the player on your first level on your project. $$anonymous$$ake shure your using C#

avatar image shane_123 · Nov 25, 2015 at 01:02 AM 0
Share

I just found how to fix parsing error you need to find

         public class NewBehaviourScript : $$anonymous$$onoBehaviour
     

You will need to change NewBehaviourScript to your script name

avatar image Le-Pampelmuse shane_123 · Nov 25, 2015 at 07:52 AM 0
Share

Again: if needs to be lower case. Otherwise it will result in an error.

Yes the file name must match the class name. But that's not relevant here.

I suggest you have a look at the tutorials: IF Statements. ;)

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

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

Related Questions

Any way to use UI on a screen renderer? 0 Answers

My script does not work each time i enter Unity unless i add it to the object again. any solutions? 0 Answers

How to implement onTriggerEnter and onTriggerExit in Vuforia. 0 Answers

Slicing an int in js/unityscript? 1 Answer

Inventory System: NullReferenceException Error 1 Answer


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