Hey I need some help with my inventory script. The GUI script for my inventory is generating an error:
Assets/Scripts/Hero/InventoryGUI.js(53,33): BCE0005: Unknown identifier: ‘Item’.
PickupItem.js:
#pragma strict
public var interactionDistance:float = 3;
public var playerCamera:GameObject;
private var ispickup:boolean;
var inventoryIcon:Texture2D;//this is what will be displayed in the demos On Screen GUI.
var isStackable=false;//If this is true it will be stacked in the inv. if not then it wont!
var maxStack=9;//each stack can be this big.
function Start () {
}
function Update () {
var hit:RaycastHit;
Debug.DrawRay(playerCamera.transform.position, playerCamera.transform.forward * interactionDistance,Color.green);
var playersinv=FindObjectOfType(Inventory);
if(Physics.Raycast (playerCamera.transform.position, playerCamera.transform.forward, hit, interactionDistance)) {
ispickup = (hit.collider.tag == "pickup");
if(ispickup && Input.GetKeyDown(KeyCode.E)) {
playersinv.AddItem(this.transform);
MoveMeToThePlayer(playersinv.transform);//moves the object, to the player
}
}
}
function MoveMeToThePlayer(theplayer:Transform){//This will basically make the item stay where the player is
//as long as its in the players inventory. This can also be done multiple ways, but ill stick with an easy one.
transform.collider.isTrigger=true;//makes it undence.
transform.renderer.enabled=false;//makes it invisible
transform.parent=theplayer;//makes the object parented to the player.
transform.localPosition=Vector3.zero;//now that the item is parented to the player
//i can set the localPosition to 0,0,0 and it will be ontop of the player. and move around with him/her
}
function BeDropped(){//This will drop the item wherever the player is.
//since the player has the item parented to him, and at his position (locally) we are going to just
//drop the item where it already is
transform.collider.isTrigger=false;//reactive its collider.
transform.renderer.enabled=true;//allow it to be seen again.
transform.parent=null;//give it no parent.
}
Inventory.js:
#pragma strict
var Contents:Transform[];
function Start () {
}
function AddItem(Item:Transform){//Add an item to the inventory.
var newContents=new Array(Contents);
newContents.Add(Item);
Debug.Log(Item.name+" Has been added to inventroy");
Contents=newContents.ToBuiltin(Transform);
}
function RemoveItem(Item:Transform){//Removed an item from the inventory.
//print("made it here");
var newContents=new Array(Contents);
var index=0;
var shouldend=false;
for(var i:Transform in newContents){
if(i==Item){
Debug.Log(Item.name+" Has been removed from inventroy");
newContents.RemoveAt(index);
shouldend=true;
//No need to continue running through the loop since we found our item.
}
index++;//keep track of what index the item is and remove it.
if(shouldend){
Contents=newContents.ToBuiltin(Transform);
return;
}
}
}
InventoryGUI.js:
#pragma strict
//This type of inventory display will be a bag. similair to WoW.
var backDrop:Texture2D;
var windowPosition:Vector2=Vector2(200,200);//where on the screen the window will appear.
//this can easily be and should be updated on the fly incase the screen size changes or what not.
var windowSize:Vector2=Vector2(96.0,96.0);//the size of the window the bag will be displayed.
var itemIconSize:Vector2=Vector2(32.0,32.0);//The size of the item icons
var updateListDelay=1.0;//This will be used to updated the inventory on screen, rather then
//updating it every time OnGUI is called. if you prefer you can directly get what in the list. but i
//dont like having multiple GetComponents >.<.
var lastUpdate=0.0;//last time we updated the display.
var UpdatedList:Transform[];
var associatedInventory:Inventory;
function Start () {
}
function UpdateInventoryList(){
UpdatedList=associatedInventory.Contents;
}
function OnGUI(){
//THIS BLOCK OF CODE IS JUST FOR PEOPLE TO MOVE THE BOX AROUND.
//If your making a game you dont need anything this this.
windowPosition.x = int.Parse(GUI.TextField(Rect (100, 10, 40, 20), ""+windowPosition.x, 3));
windowPosition.y = int.Parse(GUI.TextField(Rect (100, 30, 40, 20), ""+windowPosition.y, 3));
windowSize.x = int.Parse(GUI.TextField(Rect (100, 50, 40, 20), ""+windowSize.x, 3));
windowSize.y = int.Parse(GUI.TextField(Rect (100, 70, 40, 20), ""+windowSize.y, 3));
itemIconSize.x = int.Parse(GUI.TextField(Rect (100, 90, 40, 20), ""+itemIconSize.x, 3));
itemIconSize.y = int.Parse(GUI.TextField(Rect (100, 110, 40, 20), ""+itemIconSize.y, 3));
GUI.Label(Rect (0, 10, 400, 20), "WindowPosition X:");
GUI.Label(Rect (0, 30, 400, 20), "WindowPosition Y");
GUI.Label(Rect (0, 50, 400, 20), "WindowSize X");
GUI.Label(Rect (0, 70, 400, 20), "WindowSize Y");
GUI.Label(Rect (0, 90, 400, 20), "ItemIconSize X");
GUI.Label(Rect (0, 110, 400, 20), "ItemIconSize Y");
//THIS IS WHERE THE EDITING STUFF ENDS> FROM HERE BEFORE YOU would need.
var currentX=windowPosition.x;//where to put the first items
var currentY=windowPosition.y;
//Draw the backdrop in the windowposition and the size of the windowsize.
GUI.DrawTexture(Rect(windowPosition.x,windowPosition.y,windowSize.x,windowSize.y),backDrop,ScaleMode.StretchToFill);
for(var i:Transform in UpdatedList){//we start a loop for whats in our list. You could
var item=i.GetComponent(Item);//we know that all objects in this list are items, cus we
//will make sure nothing else can go in here, RIGHT? :P
//directly call accocialtedInventory.Contents but i prefer not to since its more work for you and the pc.
//I use a button since its easier to be able to click it and then made a drop down menu to delete or move
if(GUI.Button(Rect(currentX,currentY,itemIconSize.x,itemIconSize.y),item.inventoryIcon)){
associatedInventory.RemoveItem(i);//Remove the item from the list, well its transform neways
item.BeDropped();//Drops the item.
lastUpdate=0.0;//Set the lastupdate to 0 to allow the list to update.
}
currentX+=itemIconSize.x;
if(currentX+itemIconSize.x>windowPosition.x+windowSize.x){
//if the next item icon will be to large for the window.....
currentX=windowPosition.x;//we move it back to its startpoint
currentY+=itemIconSize.y;//and down a row.
if(currentY+itemIconSize.y>windowPosition.y+windowSize.y){//if the row is down to far. we quit the loop
return;
}
}
}
}
function FixedUpdate(){//I will update the display inventory here.
if(Time.time>lastUpdate){
lastUpdate=Time.time+updateListDelay;
UpdateInventoryList();
}
}
Thanks for any help
Regards
Marc