- Home /
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
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?
That's right! Just to re$$anonymous$$d you, my english is not that good...
Well, i'm using a vehicule in my level, but yes I do have a collider.
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:
Create a new game object.
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.
Make sure the "Is Trigger" checkbox is checked under the collider configurations for your new area.
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); }
Add this newly created script (component) to your newly created game object.
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; }
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.
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
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) {
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.
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'
It's case sensitive. Find the place in your script that uses Loadlevel() and change it to LoadLevel(), with two capital Ls.
Answer by Luuk Holleman · Mar 03, 2013 at 09:05 PM
When you reach that point:
transform.AddComponent("path/to/script");
Your answer

Follow this Question
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