Multiple shops (135455)

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? ^^

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.

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 Answers

2

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.

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)

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?

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.

Thank you it did work. But just a quick question, is it just the boolean that makes it so it works with multiple shops?

No problem, glad it worked out! Boolean is there only for optimization purposes. The reason it works with multiple shopkeepers is that Unity creates a new instance of this monobehaviour class for every attached game object. Therefore each instance can keep their own values and states, instead of sharing same properties. For more information about this concept, you can google "Object oriented programming".

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.