- Home /
How to debug this shopkeeper script ?
Hello, I am making a RPG game but as I am new to unity then I stumbled on a problem that I cannot figure out myself. I made a shopkeeper script that would work for many shopkeepers. The problem is that when I click different shopkeepers they all give the same layout of items although I have but different numbers for the script on different shopkeepers. At the beginning I thought that it isn't working at all but then I realized that although the layout is always the same the items are as I put them for every shopkeeper. So it kinda works only the layout of items has the problem.
var hit : RaycastHit;
static var shopOpen : boolean = false;
var texture : Texture2D;
var shopSize = Rect(50, 50, 400, 500);
var items : int[]; //This is where I put the item numbers for every shopkeeper
static var shopList : List.<ItemClass> = new List.<ItemClass>(); // this is where all the items for shops are listed
var myTransform : Transform; //current transform data of this enemy
var target : Transform;
var shopDistance : int = 5;
private var slotNR : int = 0;
function Awake (){
myTransform = transform; //cache transform data for easy access/preformance
}
function Start (){
target = GameObject.FindWithTag("Player").transform; //target the player
}
function OnGUI () {
if(shopOpen == true){
shopSize = GUI.Window(3, shopSize, ShopWin, texture);
}
}
function Update () {
var distance = (target.position - myTransform.position).magnitude;
if(distance < shopDistance){
if(Input.GetMouseButtonDown(0) &&
collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) {
//enable shop
shopOpen = true;
}
}
// if(distance > shopDistance){
// shopOpen = false;
// }
}
function ShopWin(){
if(GUI.Button(Rect(375, 0, 25, 25), "X")){
shopOpen = false;
}
// For recognizing the slot
slotNR = 0;
//y is row nr
for(var y = 0; y < 2; y++){
GUILayout.BeginHorizontal();
//x is columnt nr
for(var x = 0; x < 6; x++){
xPosition = 12 + (x * 62);
yPosition = 30 + (y * 62);
//Shop slots. If clicked they check what item it is in the slot and then corresponds to it
if(GUI.Button(Rect(xPosition, yPosition, 60, 60), shopList[items[slotNR]].icon)){
Inventory.Coins -= 50;
Inventory.playerInventory.Add(shopList[items[slotNR]]);
return;
}
slotNR++;
}
GUILayout.EndHorizontal();
}
GUI.DragWindow();
}
I would be very thankful for any kind of hints :)
It just because you put a "static" type of List, that means all your shop keeper will store the same information about how much items would be sold, the prices, and amounts,... , and believe me, if you bought all things from one store, you wouldn't see anything in other stores.
But there is a list of items(numbers) that will be different for each shop. And because of these numbers I can choose what I want to sell in what shop. it is just that the icons are the same. Even at the moment I can buy different things from different shops just that I need a way that the icons would be refreshed when I open another shop. :) And making a different script or list for every shop would be kind of wasting the resources, or am I wrong ?
Think about resources here is too soon. But if that's your idea, my suggestion is only about making a new script that store informations about what to sell in each shop, and your shopList will get information from that ins$$anonymous$$d of making it static so every shop sell the same things at the same time
Okay. I will try it this way. Thank you for answering :)
@$$anonymous$$ariosep
Could I have your player inventory script? And your ItemClass script? I'd like to see how they work since I'm making a game that has this kind of stuff.
Your answer
![](https://koobas.hobune.stream/wayback/20220613111708im_/https://answers.unity.com/themes/thub/images/avi.jpg)