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 ite144 · Dec 28, 2011 at 10:30 PM · storeshopmarket

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

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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

Delete object when two collision or more 1 Answer


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