I’m making a game where I have multiple shops. I was trying to us distance to check if I was in range, and then display a little text saying “Press E to talk” or something like that.
But when I tested it, i realized that you can only do that with one shop.
So is there another way to have multiple shops? if there is, could you explain in steps?
using System.Collections.Generic;
using UnityEngine;
public class Shop : MonoBehaviour
{
static List<Shop> allShops = new List<Shop>();
void Awake()
{
allShops.Add(this);
}
public static Shop FindClosestShop(Vector3 targetPosition)
{
float closestDistance = float.MaxValue;
Shop closestShop = null;
foreach (Shop shop in allShops)
{
float distanceToTargetPosition = Vector3.Distance(shop.transform.position, targetPosition);
if (distanceToTargetPosition < closestDistance)
{
closestDistance = distanceToTargetPosition;
closestShop = shop;
}
}
if (closestShop != null)
{
Debug.Log("Found closest shop called " + closestShop.gameObject.name + ", at distance " + closestDistance);
}
return closestShop;
}
}