Search Unity

Inventory System - from Brackeys

Discussion in 'Scripting' started by mrgohut, May 9, 2014.

  1. mrgohut

    mrgohut

    Joined:
    Feb 8, 2014
    Posts:
    134
  2. Erisat

    Erisat

    Joined:
    Jan 31, 2013
    Posts:
    88
    haven't used it but if you show us your errors and the related code maybe someone could give some insight on where the mistake is.
     
  3. mrgohut

    mrgohut

    Joined:
    Feb 8, 2014
    Posts:
    134
    Hmm okay, so first :
    when i want to pick up item, i got this error
    and this is in Inventory.cs script, this lane:
    Code (csharp):
    1.         newContents.CopyTo(Contents); //Array to unity builtin array
    (AddItem) void.
    http://pastebin.com/p7VARRKj
     
  4. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    I've run into the same problem, Apparently Contents is a public Transform[], and newContents is a local ArrayList that is attempting to .CopyTo() ... and for some reason it isn't allowing Contents to be a dynamic array and automatically update itself with the appropriate indexes? essentially the .CopyTo() function is failing.
     
  5. iamsteele

    iamsteele

    Joined:
    Mar 25, 2014
    Posts:
    29
    Doing Debug.Log (Contents.IsFixedSize); returns true, so Transform[] Contents is not a variable width array.

    Use System.Array.Resize(ref Contents, size) to resize it, and then Contents[indexofitem] to assign to it, remove the ArrayLists entirely from the code, they're rather worthless. Write out your own function for moving items to the left starting from where you remove the item under the remove function, and from where you equip it in the equip function, and so on.

    The next error you find will be a null reference when equipping, and the solution to that is in the Character.cs update

    GameObject Clone = blah

    to

    GameObject Clone;
    Clone = GameObject.Instantiate (item.equippedWeaponVersion, WeaponSlot.position, WeaponSlot.rotation) as GameObject;

    where item.equippedWeaponVersion is a public Transform, change that to GameObject in code, and update your reference to your weapon slot in the editor.
     
    Last edited: Jun 3, 2014