I made a simple inventory system where you can drag and drop items inside the inventory screen. I have an array of GameObjects (items) and an array of Rect (rects) both the same size (32). "items" is used to access the inventory texture to use of each item, and "rects" is used to place a rectangle on that texture to detect mouse input.
Here is the relevant code :
function OnGUI ()
{
// Inventory Screen is toggled
if (isToggled)
{
// Inventory Screen Box
GUI.Box (Rect (Screen.width/2+50, 50, Screen.width/2-100, Screen.height-100), "Inventory");
boxRect = Rect (Screen.width/2+50, 50, Screen.width/2-100, Screen.height-100);
// Begin rendering item textures in the inventory
for (var i = 0; i < 4; i++)
{
for (var j = 0; j < 8; j++)
{
// If the item exists in the array and hasn't been selected (or dragged), draw its texture in the original position
if (items[(8*i+j)] && items[(8*i+j)].gameObject.GetComponent (WeaponStats).drawOldPos == true
&& items[(8*i+j)].gameObject.GetComponent (WeaponStats).selected == false)
{
var texWidth = items[(8*i+j)].gameObject.GetComponent (WeaponStats).baseImage.width;
var texHeight = items[(8*i+j)].gameObject.GetComponent (WeaponStats).baseImage.height;
rects[(8*i+j)] = Rect (texWidth * j + Screen.width/2+60, Screen.height - (texHeight * i + 105), texWidth, texHeight);
GUI.DrawTexture (Rect (texWidth * j + Screen.width/2+60, (texHeight * i + 70), texWidth, texHeight),
items*.gameObject.GetComponent (WeaponStats).baseImage, ScaleMode.StretchToFill, true);*
*}*
*// If the item exists...*
_if (items[(8*i+j)])_
*{*
*// If the mouse button is Down and the item is selected, the item texture follows the mouse*
_if (mDown && items[(8*i+j)].gameObject.GetComponent (WeaponStats).selected == true)_
*{*
_/* 1 */GUI.DrawTexture (Rect (Input.mousePosition.x - texWidth/2, Screen.height - Input.mousePosition.y - texHeight/2,_
_texWidth, texHeight), items[(8*i+j)].gameObject.GetComponent (WeaponStats).baseImage, ScaleMode.StretchToFill, true);_
*}*
*// If the item has been dragged and isn't selected anymore (MouseButtonUp after dragging the item),*
*// draw item texture where the mouse button has been released (guiPosition)*
_if (items[(8*i+j)].gameObject.GetComponent (WeaponStats).drawOldPos == false &&_
_items[(8*i+j)].gameObject.GetComponent (WeaponStats).selected == false)_
*{*
_/* 2 */GUI.DrawTexture (Rect (items[(8*i+j)].gameObject.GetComponent (WeaponStats).guiPosition.x - texWidth/2,_
_Screen.height - items[(8*i+j)].gameObject.GetComponent (WeaponStats).guiPosition.y - texHeight/2, texWidth, texHeight),_
_items[(8*i+j)].gameObject.GetComponent (WeaponStats).baseImage, ScaleMode.StretchToFill, true);_
_/* 3 */rects[(8*i+j)] = Rect (items[(8*i+j)].gameObject.GetComponent (WeaponStats).guiPosition.x - texWidth/2,_
_items[(8*i+j)].gameObject.GetComponent (WeaponStats).guiPosition.y - texHeight/2, texWidth, texHeight);_
*}*
*}*
*}*
*}*
*}*
*}*
*```*
*<p>Everything works well, but if I try to select the <strong>last item that wasn't dragged</strong>, Unity crashes. So if I have 1 item in the inventory and I select it, it crashes. If I have 3 items, I can move any 2 items but moving a third one causes a crash.</p>*
*<p>Commenting the lines 1, 2 and 3 (see commented numbers in code above) prevents the crash from happening, but I obviously need those lines.</p>*
*<p>I hope I gave enough information. This problem caused me numerous headaches, so thanks for any help.</p>*