Ok so i’m trying to create a very simple inventory and vendor system using the scripts below. Right now, i’m stuck at the point where i want to just click on an item from the vendor window and then the GUI button of this item disappears from him and appears in the player’s inventory. I tried to use the “GetComponent” method which i’ve used successfully on other scripts but with no luck. Is there any other way to do such a thing? Of course, i’d love to have a drag-and-drop system but that’s a little more advanced than what i want right now. Thanks!
Vendor Script.js
var range : float;
var player : GameObject;
var close : boolean = false;
var vendorwindow : boolean = false;
function OnMouseOver(){
if((Input.GetMouseButtonDown(0)) && close){
vendorwindow = true;
}
}
function Update(){
if(Vector3.Distance(player.transform.position, transform.position)<=range){
close = true;
}
else{
close = false;
vendorwindow = false;
}
}
function OnGUI(){
if(vendorwindow){
GUI.Box(new Rect(20,10,160,160), "SHOP");
if(GUI.Button(new Rect(40,40,50,40),"Sword")){
Debug.Log("I bought a sword!");
}
if(GUI.Button(new Rect(40,90,50,40),"Shield")){
Debug.Log("I bought a shield!");
}
if(GUI.Button(new Rect(110,40,50,40),"Bow")){
Debug.Log("I bought a bow!");
}
if(GUI.Button(new Rect(110,90,50,40),"Arrows")){
Debug.Log("I bought some arrows!");
}
}
}
Player Inventory.js
var openINV : boolean = false;
function Update(){
if(Input.GetKeyDown(KeyCode.I)){
openINV = !openINV;
}
}
function OnGUI(){
if(openINV){
GUI.Box(new Rect(Screen.width - 180,Screen.height - 180,160,160),"INVENTORY");
GUI.Box(new Rect(Screen.width - 160,Screen.height - 150,50,40),"");
GUI.Box(new Rect(Screen.width - 160,Screen.height - 100,50,40),"");
GUI.Box(new Rect(Screen.width - 90,Screen.height - 150,50,40),"");
GUI.Box(new Rect(Screen.width - 90,Screen.height - 100,50,40),"");
}
}