Search Unity

merchant inventory issues [probably solved]

Discussion in 'Scripting' started by gibberingmouther, Nov 16, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    i keep getting an array index out of range exception on the Inv_Buy line. the size is set to 4 in the inspector on the merchant object who has the merchant inventory script attached. the Debug.Log says the inventory is set to 0, which explains the out of range error. i access the Merchant1 script (which contains the public inventory) in this script using getcomponent. not sure what i'm doing wrong. will show more code as requested. i access the hero inventory the same way in other scripts - should i be using get components in children or something? ugh.

    Code (csharp):
    1.  
    2. public void OnPointerClick(PointerEventData eventData)
    3.         {
    4.             pos = theText3.transform.position;
    5.             //offset
    6.             pos.x += 5;
    7.             pos.y += 80;
    8.  
    9.         if (eventData.button == PointerEventData.InputButton.Left)
    10.             {
    11.                 Globals.merc1index = StringtoIndex(theText3.name);
    12.                 if (MouseOverMerchant1.buybutton)
    13.                 {
    14.                     //Debug.Log("in buy button");
    15.                     //merc1.Inv_Buy(merc1.merchant1[Globals.merc1index].item);    // out of range?
    16.                     Debug.Log(merc1.merchant1.Length);
    17.                     merc1.Inv_Buy(merc1.merchant1[2].item); // still out of range
    18.                 }
    19.             }
    20.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you get zero, I'm not sure what the problem is (as far as understanding what's wrong, I mean)?
     
  3. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    well the array is supposed to be set to, in this case, 4, which is what the array is set to in the inspector. i want that value, not 0. maybe i'm using getcomponent wrong?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, maybe but unless you have another merc1 or what not, getcomponent should return null if it's wrong, therefore not print out 0 , I would think.
     
  5. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    here's some code if that can help elucidate the problem:
    Code (csharp):
    1.  
    2.         public Text theText3;
    3.         public GameObject hasstoreattached2;
    4.         private Merchant1 merc1;
    5.  
    6.         public Canvas MerchRightClickCnv;
    7.         public Transform MerchRightClick;
    8.  
    9.         private Vector3 pos;
    10.  
    11.         public Canvas ExamineCanvas;
    12.         public Text examinetitletext;
    13.         public Text examinetext;
    14.         public Transform examinemove;
    15.  
    16.         private void Start()
    17.         {
    18.             ExamineCanvas.enabled = false;
    19.             merc1 = hasstoreattached2.GetComponent<Merchant1>();
    20.  
    21.             MerchRightClickCnv.enabled = false;
    22.         }
    23.  
    http://i374.photobucket.com/albums/oo186/gcyoung1234/inventoryissuesmerc1_zpsbuempoj2.png

    this is a picture of the object's inspector with the Merchant1 script attached
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I dunno, but I would start with debug log to see it's not null on get component. I would turn the fields public or serializeField and then when it's running click on them in the inspector to make sure they are what you think they are, etc.. try to find out what's wrong ?! lol
     
    gibberingmouther likes this.
  7. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    instead of the hasstoreattached2 line above, i tried:
    merc1.merchant1 = hasstoreattached2.GetComponentsInChildren<ItemHolder>();
    and
    merc1.merchant1 = store1.GetComponentsInChildren<ItemHolder>();

    now instead of an array index out of bounds error i'm getting a bunch of null reference exceptions. i would have thought one of these would do the job, but apparently not. if i were to initialize the array in the inspector, i think that would work, but that isn't an option with the way i laid out my inventory. plus it's not desirable for other reasons. ugh ... any ideas, anyone? i so hate getting stuck like this and just hoping there's a solution, but i guess that's part of this kind of work.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When you say initialized... you mean filled or something else? In your screenshot, it's length was 4 -- they were all just null/empty, right?

    Did you look in the inspector, and click on the variables & see what they highlighted in the hierarchy and was all as you imagined/thought it should be? If you want (and can make a small copy of the problem).. you could attach it here, I'd look at it when I'm free.
     
  9. inthemilkywat

    inthemilkywat

    Joined:
    Dec 29, 2013
    Posts:
    56
    Can you show how you initialized the array in merchant1 class? Make sure you set the length in awake or start.
     
    gibberingmouther likes this.
  10. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    i have this code in the start method of the merchant script:
    merchant1 = store1_1.GetComponentsInChildren<ItemHolder>();
    i actually was doing that wrong ... i had the wrong reference to the store. so i'll tinker with things tomorrow - maybe that was the problem. thanks for looking at my thread!
     
  11. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    buying now works! the array out of bounds error was solved. still got to get selling to work and some other issues with displaying the inventory.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad ya got it working some more..
     
  13. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    now for selling, i'm getting this exception:

    ArgumentOutOfRangeException: startIndex + length > this.length
    Parameter name: length
     
  14. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    fixed that, now i'm further along. still have to work out a lot of kinks in the system.

    one problem i need help with is as follows. i have the "ShowInventory" function in my heroInventory script, that gets called if "inventoryflag" is true (called from update). inventoryflag is a public static bool. in another script i set it to true, but the inventory isn't being displayed when i do this? what's wrong? since it's in update and i have the object with the script attached in the scene, it should trigger the next frame, right?

    Code (csharp):
    1.  
    2.  void Update()
    3.         {
    4.             if (inventoryflag)
    5.             {
    6.                 ShowInventory();
    7.             }
    8.         }
    9.  
    edit;Solved: ShowInventory doesn't actually enable the inventory canvas, so i had to do that separately. fixed!

    edit2-moresolved: got the store working, both buying and selling from that one merchant. yipee!
     
    Last edited: Nov 20, 2017