Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Modifying script for equipment

Discussion in 'Scripting' started by Corva-Nocta, May 20, 2016.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I'm following a tutorial series to create an inventory and equipment system. So far everything has been perfect as far as the UI is concerned, but actually instantiating the objects is giving ne just a little trouble. I'm also not sure which part of the code would need to be changed. As far as I can tell it should work. I'll try to add as much relevant code as I can.

    The first script which controls the equipped items UI
    Code (csharp):
    1.  public void OnPointerDown (PointerEventData)
    2. {
    3. if (inventory.draggingItem)
    4. {
    5. if (index == 2 && inventory.draggedItem.itemType == ItemItemType.MainHand)
    6. {
    7. Item temp = item;
    8. item = item = inventory.draggedItem;
    9. inventory.shiwDraggedItem (temp, -1)
    10.  
    11. playerEquipment.EquipHand (item = inventory.draggedItem);
    The last line of code is referencing the second script which will control things like stats, speed, and any other combat infor I might need. Here is the code for that script:
    Code (csharp):
    1. public void EquipHand (Item item)
    2. {
    3. GameObject itemAsGameObject = (GameObject)Instantiate (Item.itemModel, GameObject.FindObjectWithTag ("Player").transform.position, Quaternion.identity);
    4. itemAsGameObject.GetComponent <DroppedItem>().item = Item;
    I'm not sure where the problem is, but the compile log says the issues are with the second script. Specifically the Item.itemModel next to the Instantiate and the very last Item.

    Any thoughts?
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Capitalization is important! ;) "Item" is the name of the class, "item" is the name of the variable. It should be:
    Code (csharp):
    1. public void EquipHand (Item item)
    2. {
    3. GameObject itemAsGameObject = (GameObject)Instantiate (item.itemModel, GameObject.FindObjectWithTag ("Player").transform.position, Quaternion.identity);
    4. itemAsGameObject.GetComponent <DroppedItem>().item = item;
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Wow, literally all it took to fix it. Thanks a ton!
     
  4. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    Hm, I do get a null reference when I spawn the item. Still better than nothing though
     
  5. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Make sure your player actually has the tag "Player" and make sure the item you're passing in actually has a model in the itemModel field.