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
1
Question by crusherxman · Mar 03, 2013 at 07:21 PM · scenetimerchangeenableplace

How do I enable a script when reaching a place

Hello everyone! I'm currently working on a horror game on my very first level. I've managed to create a GUI text when reaching a place but I had a second script that when I press a key, my scene would change and the problem is when i'm not at the "point" of my level even though if I press the same key from my second script, my scene still changes. So yes, what i'm looking for is a script to when I reach to my location, I want my script to be enabled (While my script is disabled)

Here's a exemple of what I want: As I am walking through a dark corridor when I press the "E" button, I cannot instantly change scenes but when I finally reach a point, I can now use my key to change my scene to the next level.

Before you think, i've used the Unity engine for 6 months, I have a few skills to edit and create, but my biggest problems are "scripting" and "programming", I kinda new at this, i'm just getting started :)

So anyway, if you guys have any kind of script that are vaild to this idea or else a another question around this community that looks familiar to this post, please post them below, I would apreciate anything (If it's working well, of course).

Thanks!

~crusherxman

Comment
Add comment · Show 6
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 pazzesco · Mar 03, 2013 at 07:51 PM 0
Share

Hey crusherxman, just want to clarify what you're looking to do;

Essentially, you want to restrict the action of a key being pressed until a specific object (such as the player) reaches a certain point within the level?

avatar image crusherxman · Mar 03, 2013 at 07:52 PM 0
Share

That's right! Just to re$$anonymous$$d you, my english is not that good...

avatar image pazzesco · Mar 03, 2013 at 07:55 PM 0
Share

Does your player object have a collider?

avatar image crusherxman · Mar 03, 2013 at 07:58 PM 0
Share

Well, i'm using a vehicule in my level, but yes I do have a collider.

avatar image pazzesco · Mar 03, 2013 at 08:06 PM 0
Share

One sec, typing up an answer

Show more comments

2 Replies

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

Answer by pazzesco · Mar 03, 2013 at 08:45 PM

What you can do in this situation is create a trigger collision area, and then once the player walks into that area, have it set a flag within the script you use to handle player input. I'd recommend the following steps for implementing this solution:

  1. Create a new game object.

  2. Add a collider component to this new game object. It can whatever shape you want it to be. This collider is going to represent the area that enables your player to press the "E" button (or whatever button you chose). Make sure the game object and collider are adequate size and correctly positioned.

  3. Make sure the "Is Trigger" checkbox is checked under the collider configurations for your new area.

  4. Create a new script (Javascript). Erase the default code within it. Then paste this code into it:

    pragma strict

    //This function will tell the object entering it to call the function "ToggleButton", with a boolean parameter equal to true. //For the player object, it will "let it know" that it can press the E button. function OnTriggerEnter(col:Collider) { Debug.Log("test"); col.gameObject.SendMessage("ToggleButton", true, SendMessageOptions.DontRequireReceiver); }

    //This function will tell the object exiting it to call the function "ToggleButton", with a boolean parameter equal to false. //For the player object, it will "let it know" that it cannot press the E button. function OnTriggerExit(col:Collider) { col.gameObject.SendMessage("ToggleButton", false, SendMessageOptions.DontRequireReceiver); }

  5. Add this newly created script (component) to your newly created game object.

  6. If you have a script that handles player input, then you'll want to integrate this code with that script. You can make this a separate script if you want, too. At any rate, the following code handles the key pressing:

    pragma strict

    //NOTE: Make sure your player object has a Collider, is NOT a trigger, and has a Rigidbody component.

    //NOTE: Make sure your ending area object has a Collider, is a trigger, and has the EndPointButtonToggle.js component.

    //NOTE: You can integrate this script with your player controller script. In this case, it'd be the ThirdPersonController.js script.

    private var canPressKey:boolean = false;

    function Update () {

      //This is the conditionals that allow the player to get the desired actions for pressing E.
         if (Input.GetKeyDown(KeyCode.E) && canPressKey == true) {
     
             Debug.Log("Pressed key in area");
             //do whatever else you need to do here when player's button is pressed in the area.
     
          }
     
     }
     
     //This function is called by the triggering area whenever this object enters or exits it.
     function ToggleButton(state:boolean) {
         Debug.Log("ToggleButton() called with value of " + state.ToString());
         canPressKey = state;
     }
    
    
    
  7. Optionally, you can use Input.GetButtonDown() which uses Unity's customizable inputs instead of Input.GetKeyDown(). Input.GetButtonDown() is much more flexible, but Input.GetKeyDown() will work for this demonstration. If you're going to use Input.GetButtonDown(), make sure you've setup the proper input keys to handle your button pressing. For more information on configuring input, check out this link.

Let me know if this works.

Comment
Add comment · Show 32 · 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 crusherxman · Mar 03, 2013 at 09:26 PM 0
Share

I'm having a little problem here, i've pasted every script but I keep getting this error:

UnityException: Input $$anonymous$$ey named: $$anonymous$$eyCode E is unknown at (wrapper managed-to-native) UnityEngine.Input:Get$$anonymous$$eyString(string)

$$anonymous$$y game stills run but the input is not working correctly

avatar image pazzesco · Mar 03, 2013 at 09:36 PM 0
Share

Input.GetButtonDown() is somewhat of a deceptive name. It uses mapped string names with keys in the input manager, rather than the name of actual keys themselves. $$anonymous$$ake this change:

 //...change this...
 
 if (Input.GetButtonDown("Fire1") && canPress$$anonymous$$ey == true) {
 
 
 //...to this...
 
 if (Input.Get$$anonymous$$eyDown("E") && canPress$$anonymous$$ey == true) {
avatar image pazzesco · Mar 03, 2013 at 09:38 PM 0
Share

Normally, I'd recommend using mapped names as it allows a more versatile management of key bindings. But, as a beginner script, you can get away with using Input.Get$$anonymous$$eyDown(). I'd suggest looking into the Input $$anonymous$$anager and understanding that after you get this running.

avatar image crusherxman · Mar 03, 2013 at 10:14 PM 0
Share

Alright good, i've changed my script, but now i'm having just a smaller problem: $$anonymous$$y main script (That one with the main "E" function) is kinda mest up when I play my scene:

$$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.Application.Loadlevel'

avatar image pazzesco · Mar 03, 2013 at 10:20 PM 0
Share

It's case sensitive. Find the place in your script that uses Loadlevel() and change it to LoadLevel(), with two capital Ls.

Show more comments
avatar image
0

Answer by Luuk Holleman · Mar 03, 2013 at 09:05 PM

When you reach that point:

transform.AddComponent("path/to/script");

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

14 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

Related Questions

Health script, save health in between scenes. PlayerPrefs. 2 Answers

Unity seems to be taking a long time destroying a scene? 0 Answers

Scene Change Event 1 Answer

Changing scene automatically 1 Answer

Change screen Orientation when change scene 0 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