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 :slight_smile:

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

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.

When you reach that point:

transform.AddComponent(“path/to/script”);