Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by thomas1231 · Apr 19, 2017 at 11:54 PM · nullreferenceexceptioninventorynull reference exception

Im getting NullReferenceException error

Im getting NullReferenceException error for this line this.transform.SetParent (inv.slots[slot].transform); in the item data class when trying to move one of the item in the inventory to a another slot.

here is my code public class Inventory : MonoBehaviour {

 GameObject inventoryPanel;
 GameObject slotPanel;
 ItemDatabase database;
 public GameObject inventorySlot;
 public GameObject inventoryItem;

 int slotAount;
 public List<Item> items = new List<Item>();
 public List<GameObject> slots = new List<GameObject>();

 void Start()
 {
     database = GetComponent<ItemDatabase>();

     slotAount = 25;
     inventoryPanel = GameObject.Find("Inventory Panel");
     slotPanel = inventoryPanel.transform.FindChild("Slot Panel").gameObject;

     for (int i = 0; i < slotAount; i++)
     {
         items.Add(new Item());
         slots.Add(Instantiate(inventorySlot));
         slots [i].GetComponent<Slot> ().id = i;
         slots[i].transform.SetParent(slotPanel.transform);
     }
     AddItem(2);
     AddItem(1);
 }

 public void AddItem(int id)
 {

     Item itemToAdd = database.FetchItemByID(id);
     if (itemToAdd.Stackable && CheckItem(itemToAdd))
     {
         for (int i = 0; i < items.Count; i++)
         {
             if (items[i].ID == id)
             {
                 ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
                 data.amount++;
                 data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
                 break;
             }

         }
     }
     else
     {
         for (int i = 0; i < items.Count; i++)
         {
             if (items[i].ID == -1)
             {
                 items[i] = itemToAdd;
                 GameObject itemObj = Instantiate(inventoryItem);
                 itemObj.GetComponent<ItemData>().item = itemToAdd;
                 itemObj.GetComponent<ItemData> ().slot = i;
                 itemObj.transform.SetParent(slots[i].transform);
                 itemObj.transform.position = Vector2.zero;
                 itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
                 itemObj.name = itemToAdd.Title;
                 break;
             }
         }
     }
 }

 bool CheckItem(Item item)
 {
     for (int i = 0; i < items.Count; i++)

         if (items[i].ID == item.ID)

             return true;


     return false;
 }

}

public class ItemData : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler { public Item item; public int amount; public int slot;

 private Inventory inv;
 private Vector2 offset;

 void Strat()
 {
     inv = GameObject.Find ("Inventory").GetComponent<Inventory> ();    }

 public void OnBeginDrag (PointerEventData eventData)
 {
     if (item != null) 
     {
         offset = eventData.position - new Vector2 (this.transform.position.x, this.transform.position.y);
         this.transform.SetParent (this.transform.parent.parent);
         this.transform.position = eventData.position - offset;
         GetComponent<CanvasGroup> ().blocksRaycasts = false;
     }
 }

 public void OnDrag (PointerEventData eventData)
 {
     if (item != null) 
     {
         this.transform.position = eventData.position - offset;
     }
 }

 public void OnEndDrag (PointerEventData eventData)
 {
     this.transform.SetParent (inv.slots[slot].transform);
     this.transform.position = inv.slots[slot].transform.position;
     GetComponent<CanvasGroup> ().blocksRaycasts = true;


 }

}

public class Slot : MonoBehaviour, IDropHandler { public int id; private Inventory inv;

 void Start()
 {
     inv = GameObject.Find ("Inventory").GetComponent<Inventory> ();
 }

 public void OnDrop (PointerEventData eventData)
 {
     ItemData droppeditem = eventData.pointerDrag.GetComponent<ItemData> ();
     if (inv.items [id].ID == -1) {
         inv.items [droppeditem.slot] = new Item ();
         inv.items [id] = droppeditem.item;
         droppeditem.slot = id;
     } else 
     {
         Transform item = this.transform.GetChild (0);
         item.GetComponent<ItemData> ().slot = droppeditem.slot;
         item.transform.SetParent (inv.slots[droppeditem.slot].transform);
         item.transform.position = inv.slots[droppeditem.slot].transform.position;

         inv.items [droppeditem.slot] = item.GetComponent<ItemData> ().item;
         inv.items [id] = droppeditem.item;

         droppeditem.slot = id;
         droppeditem.transform.SetParent (this.transform);
         droppeditem.transform.position = this.transform.position;


     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Vicarian · Apr 20, 2017 at 04:57 AM

Problem is the ItemData class. You mistyped Start(), so inv never takes a value and remains null. Hence, the ReferenceExceptions.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image thomas1231 · Apr 20, 2017 at 06:41 AM 0
Share

I love you

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

101 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inventory problem 0 Answers

List remains null after initialisation? 1 Answer

null reference exception help 1 Answer

Null Reference exception with custom class object 0 Answers

Bogus Null Reference in Build only,Bogus Phony NULL REFERENCE in BUILD ONLY (works in editor) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges