I’m writing up a basic inventory script based on an array of GameObjects. This is just coding practise, so I’ll probably just release it when I’m done for others to use. This is my Inventory script, applied to a standard First Person Controller:
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour
{
public bool inventoryScreenEnabled = false;
public bool isHoldingSomething = false;
public GameObject heldItem = null;
public int maxSlots = 9;
public GameObject[] InventoryContents = new GameObject[9];
public Transform holdPos;
void Start()
{
Screen.showCursor = false;
Screen.lockCursor = true;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.M))
{
if (inventoryScreenEnabled == false && heldItem == null)
{
EnableInventory();
}
else if (inventoryScreenEnabled == true)
{
DisableInventory();
}
}
if (Input.GetButtonDown("Fire2") && heldItem != null)
{
heldItem.transform.parent = null;
heldItem.GetComponent<PickUp>().enabled = true;
heldItem.GetComponent<Rigidbody>().useGravity = true;
heldItem.GetComponent<Rigidbody>().isKinematic = false;
heldItem = null;
}
}
void EnableInventory()
{
this.transform.GetComponent<CharacterMotor>().enabled = false;
this.transform.GetComponent<MouseLook>().enabled = false;
Camera.mainCamera.GetComponent<MouseLook>().enabled = false;
inventoryScreenEnabled = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
void DisableInventory()
{
this.transform.GetComponent<CharacterMotor>().enabled = true;
this.transform.GetComponent<MouseLook>().enabled = true;
Camera.mainCamera.GetComponent<MouseLook>().enabled = true;
inventoryScreenEnabled = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
void OnGUI()
{
if (inventoryScreenEnabled == true)
{
GUI.BeginGroup (new Rect (Screen.width / 2 - 150, Screen.height / 2 - 150, 300, 300));
GUI.Box(new Rect(0,0,300,300), "Inventory");
for(var i = 0; i < 9; i++)
{
if (InventoryContents *!= null)*
-
{*
_ if (GUI.Button(new Rect(10,30,280,25), InventoryContents*.name))_
_ {_
_ Debug.Log(“Item pressed!”);_
_ heldItem = InventoryContents;
InventoryContents = null;
heldItem.SetActive(true);
heldItem.transform.parent = holdPos;
heldItem.transform.position = holdPos.transform.position;
heldItem.transform.rotation = holdPos.transform.rotation;
heldItem.GetComponent().useGravity = false;
heldItem.GetComponent().isKinematic = true;
DisableInventory();
}
}
}
}
}
}*
And this is the PickUp script, applied to each item that can be added to the inventory:
using UnityEngine;
using System.Collections;_
public class PickUp : MonoBehaviour
{
* public GameObject playerObject;*
* private Inventory inventory;*
* void Start()*
* {*
* inventory = playerObject.GetComponent(); *
* }*
* void OnMouseDown()*
* {*
* if (inventory.inventoryScreenEnabled == false && inventory.heldItem == null)*
* {*
* for(var i = 0; i < inventory.maxSlots; i++)*
* {*
_ if (inventory.InventoryContents == null)
* {
inventory.InventoryContents = this.gameObject;
transform.parent = playerObject.transform;
transform.position = playerObject.transform.position;
enabled = false;
this.gameObject.SetActive(false);
}
}
}
}
}*
It all works fine until I actually pick up an object, at which point I get that error: “Array index is out of range.”
Can anyone tell me what I’m doing wrong?_