• 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
Question by Bokaii · Apr 07, 2015 at 06:43 AM · unity 5shop

Multiple shops

  Using UnityEngine.UI;
  
  public Text display;
  public string displayText;
  
  void Update(){
  float dist = Vector3.Distance(transform.position, player.position);
  
  if(dist<=3){
      display.text = displayText;
  }
  else{
      display.text = "";
  }
  }

 

If I attach the script to the shopkeepers, logically it doesn't work. But is there another way of doing this? ^^

Comment

People who like this

0 Show 2
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
avatar image kevinspawner · Apr 07, 2015 at 06:54 AM 0
Share

Am not sure I did understood your question completely. If you want to display more than one shop, you need to create array of text.

avatar image Bokaii · Apr 07, 2015 at 07:01 AM 0
Share

If I want multiple shops, and when I get close to one of them, it displays a text. Like for weaponshop: "Press E To Enter Weapon Shop" And for some other shop: "Press E To Enter Another Shop?"

Something like that?

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by ethestel · Apr 07, 2015 at 07:42 AM

Hello,

Your algorithm is correct, however I would like to suggest a few things:

  • You didn't specified what "player" field references to. If it is a game object, then you should use player.transform.position instead of player.position.

  • You are calling "display" objects "text" setter function with the same string on each Update call, which is ideally called 60 times per second. I would recommend that you only update text when players distance status changes.

Comment
Bokaii

People who like this

1 Show 6 · 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
avatar image Bokaii · Apr 07, 2015 at 08:07 AM 0
Share

Well how would I do this?

  • And the player object is a Transform, because I'm only using it as a transform (for the position)

avatar image ethestel · Apr 07, 2015 at 08:36 AM 0
Share

Here you go, hope this helps:

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Shopkeeper : MonoBehaviour {
 
     public Text display;
     public string displayText;
     public Transform player;
     //this field will store if the text is on screen
     private bool m_isTextShown;
 
     void Start()
     {
         //set initial text
         UpdateText(false);
     }
 
     void Update(){
         float dist = Vector3.Distance(transform.position, player.position);
 
         //show text if player is in distance AND text is not on screen
         if(dist <= 3f  &&  ! m_isTextShown)
         {
             UpdateText(true);
         }
         //hide text if player is out of distance AND text is on screen
         else if(dist > 3f  &&  m_isTextShown)
         {
             UpdateText(false);
         }
     }
 
     private void UpdateText(bool showText)
     {
         Debug.Log("update");
         m_isTextShown = showText;
         display.text = (showText) ? displayText : "";
     }
 
 }
     
avatar image Bokaii · Apr 07, 2015 at 10:19 AM 0
Share

But will this not do the exact same, and if I add the script to multiple objects/ shopkeepers, only 1 of them will work and display the text?

avatar image ethestel · Apr 07, 2015 at 10:55 AM 0
Share

The result is same as yours, shopkeeper displays a text when player gets closer. But addressing the issue i reminded on my original answer, this is much more efficient than replacing text field around 60 times each second with same string.

It will work fine with multiple shopkeepers and single player, attach this script on each shopkeeper game object, and target the player on each of them.

avatar image Bokaii · Apr 07, 2015 at 12:32 PM 0
Share

Thank you it did work.

But just a quick question, is it just the boolean that makes it so it works with multiple shops?

Show more comments
avatar image

Answer by Mrmudweasel · Apr 07, 2015 at 12:59 PM

If I where you I would go the more simpler route and use OnTriggerEnter and attach it to a invisible cube with the box collider set to trigger. in the vicinity of the shopkeeper where you want the player to use the shop that the player walks into. I'll create a simple example for you.

 using UnityEngine;
 using System.Collections;
 
 public class Shopkeeper : MonoBehaviour {
     bool activated = false;
     void OnGUI () {
         if (activated == true) {
                         Debug.Log ("Welcome to the store");
                 }
     }
     
     // Update is called once per frame
     void OnTriggerEnter (Collider theCollision) {
         if (theCollision.tag == "Player")
             activated = true;
         else
             activated = false;
     }
 }

Replace the OnGUI if debug line to your shopkeeper's gui script. when the player walks up to the shopkeeper it will display your gui code and when the player walks away from the shop keeper it will remove the gui. Also remember to set the tag of your Player to Player for this to work. For any other DIFFERENT shop you want to create just copy the script, change the class name and the gui code. if you want to create another shop of the SAME kind just create the shop keeper again and the invisible cube with the box collider set to trigger and put the shopkeeper script above on it with your edited gui code. ALSO PLEASE don't forget to upvote this if it helped you, it took me some time to make this.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

22 People are following this question.

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

Related Questions

How to save coins with PlayerPrefs 2 Answers

Multiple shops 1 Answer

How can i change the sprite image of button in run time of a 2d game 1 Answer

How does addrelativeforce work? 1 Answer

How do you merge seperate 3D models that make a whole shape into one asset to drag? 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