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. = new List.(); // 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 :)
↧