Script supposed to only work at a certain area

Hi

I Have this “store script” so when the player press “b” a market menu is showing and the player can buy items.
Im trying to make it so that the market only can be opened in 1 special building at the moment players can open it everywhere.

Can someone give me a point in right direction?

You will want to add a trigger. Create a trigger the size of the area you want the player to be able to open the store.

Use the OnTriggerEnter and OnTriggerExit to set a variable in your player object (“bool isInStore”) to either true or false depending on whether the player is inside that area. You will need to check the tag of the object entering the trigger (“Player” or whatever you are using for your player), and you will have to make sure your player object has a rigidbody attached (if it doesn’t already).

Then when they press “b” you check " if (player.isInStore)" and only open if that conditional succeeds.

Thx shane.

I Got this Store script attatch too a empty gameobject, With a Box collider marked as trigger inside the storebuilding. Do i need to put the ontriggerenter and exit script on the player? or the store Object? thx for Your fast reply

set your characters tag to Player in the inspector
go into edit/project setting/input and set up input for keyboard button called “Action”

for java

var playerInStore = false;

function Update () {
if (Input.GetButtonDown(“Action”) playerInStore){
//Then whatever you want to happen e.g open shop menu
}
}

function OnTriggerEnter(Player : Collider) {
playerInStore = true;
}

function OnTriggerExit(Player : Collider) {
playerInStore = false;
}

oh yeah put it on an object with a collider and set it to isTrigger

Thx peril

//Then whatever you want to happen e.g open shop menu
At this Place can i use Getcomponent too open the store? or what do you recommand?

add the following to your current script in the appropriate places

var targetObj: Transform; //Use the inspector to drag and drop your shop into here from the hierachy

function Update () {
//Assuming your shop script is called shop script this will work
//otherwise no worries just change them both to whatever your script is called!
var otherScript: ShopScript = targetObj.GetComponent(ShopScript);
otherScript.OpenMenu();
}

in your shop object make a function called “OpenMenu” and your collision script will trigger the function in your shop script.

EXAMPLE:
function OpenMenu () {
//and put your shop menu script in here!
}

I think that should work

Thank you everyone for the help, i made it work now :slight_smile: