- Home /
A couple problems with my shop script.
Hello, I am having some trouble with a shop script i am working on. I want the player to have to be within a certain distance of the store to be prompted to press the action button (referred to as pickup) to open the shops interface. i have accomplished this by getting the distance between the player & an empty game object linked to the script that is the center of the area the player needs to be in to use the shop and then checking if the player has pressed the action button. here is me code:
ar newSkin : GUISkin;
var Shop_Name = "Shop";
var item_Pictures : Texture2D[];
var item_Price : String[];
var item_Disc : String[];
var selected = "";
var Tab = 0;
var TabNames : String[] = ["Buy", "Sell", "Repair"];
var scrollPosition : Vector2 = Vector2.zero;
var items : GameObject[];
var Shop : GameObject;
var shown = 0;
var key = Input.GetKey("Pickup");
var Shop_CanOpen : boolean = false;
var Shop_Open : boolean = false;
var Shop_prompt : boolean = false;
var Local_Player : GameObject;
var Shop_Range : float = 100;
var distance : float ;
function Start(){
Local_Player = GameObject.FindWithTag("Player");
items = new GameObject[100];
}
//buy menu
function BuyMenu () {
scrollPosition = GUI.BeginScrollView (Rect(50, 200, items.Length * 100, 400), scrollPosition, Rect (0, 0, 100, 375));
for (var i = 0; i <= items.length; i++) {
GUI.BeginGroup(Rect(25, i * 120 + 10, 450, 120));
GUI.Box(Rect(0, 0, 450, 120), "");
GUI.Box(Rect(15, 15, 120, 100), item_Pictures[i]);
GUI.Box(Rect(15, 145, 155, 75), item_Disc[i]);
GUI.Label(Rect(90, 145, 50, 25), "$" + item_Price[i]);
GUI.EndGroup();
}
}
//sell menu
function SellMenu () {
//too be added
}
//repair menu
function RepairMenu () {
//too be added
}
function theFirstMenu() {
toolbar = GUI.Toolbar (Rect(Screen.width / 2 - 300, 25, 200, 30), Tab, TabNames);
GUI.Box(Rect(Screen.width / 2 + 300, 25, 30, 30), "X");
GUI.Box(Rect(Screen.width / 2 - 100 , 25, 200, 30), Shop_Name);
//basic shop menu shtuff
GUI.BeginGroup(Rect(Screen.width / 2 - 300, 55, 600, 500));
GUI.Box(Rect(0, 0, 600, 500), "");
switch (Tab) {
case 0:
BuyMenu();
break;
case 1:
SellMenu();
break;
case 2:
RepairMenu();
break;
}
GUI.EndGroup();
}
function OnGUI () {
GUI.skin = newSkin;
if (Shop_CanOpen == true) {
if (Shop_prompt == true){
GUI.Box(Rect(Screen.width /2, Screen.height * 0.75, 100, 15), "Press " + key + " to enter Shop");
}
if (Input.GetKeyDown ("Pickup")) {
Shop_Open = true;
Shop_prompt = false;
}
}
if (Shop_Open == true) {
toolbar = GUI.Toolbar (Rect(Screen.width / 2 - 300, 25, 200, 30), Tab, TabNames);
theFirstMenu();
if (Input.GetKeyDown ("Escape")) {
Shop_Open = false;
Shop_prompt = true;
}
}
}
function Update () {
//shutff
Trans = Local_Player.transform;
distance = Vector3.Distance(Local_Player.transform.position, Shop.transform.position) ;
if(distance <= Shop_Range){
Shop_CanOpen = true;
Shop_prompt = true;
}
if (distance > Shop_Range) {
Shop_CanOpen = false;
Shop_prompt = false;
}
}
`
The problems:
-When you get in range the frame rate drops dramatically (from ~115 fps to about 5 fps) i suspect it is due to the script having to check that the player is still in range & then it has to draw the appropriate GUI. (originally i tried to use a collision detector to see if the player wa in range, but was never able to get it to work, i think that if i could get it to work it would fix the problem.)
-when your in range i want it to say "Press to enter Shop" instead it says "Press to enter Shop"(I know why it does this but i dint know how to fix it)
If someone could take the time to help me that would be great. Sorry if my Questions were too long, i am just trying to help who ever is helping me out
Answer by aldonaletto · Dec 29, 2011 at 02:15 AM
I suppose the framerate drop is due to the several runtime errors that this code produces. The errors I found are:
1- var key = Input.GetKey("Pickup"); doesn't make sense: you should assign the name of the key you want to use; if the key is space, declare var key = "space" (check the available key names in the Input Section).
2- Change the line if (Input.GetKey("Pickup")) to if (Input.GetKey(key));
3- "Escape" isn't a valid key name: it should be "escape";
4- Function BuyMenu has a BeginScrollView at the beginning, but doesn't have a EndScrollView at the end, and in the for you're comparing i , but should compare i < items.length to avoid out of range errors;
5- Some rectangles are badly sized - the pictures cover the prices, for instance;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Need help with a shop script.. :/ 1 Answer
My buttons do not appear when paused? 2 Answers
Need a bit of help with my script (about 100 lines) 1 Answer