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 Lakeyour · Jan 23, 2016 at 08:48 PM · c#inventorycollision2dinventory system

Inventory Script

Hey folks, I've been working on this script for a shorter period of time and I would like some assistance with the construction.

I'm getting an object reference error with this line of code

 item.transform.Translate (0, 0, 0);

(NullReferenceException: Object reference not set to an instance of an object TakeItems.Update () (at Assets/Scripts/Players/TakeItems.cs:22)

I would like to setup some simple 2D collision detection, otherwise I can loot items in a weird order such as this:

  1. Let's say I have a few potions in the world, let's say three for this example

  2. If I don't have collision for the looting I'm going to loot a random one, and ofcourse we want to loot the one that we actually collide with and an item that we are close to.

I have tried several OnTrigger events and OnCollision but I can't seem to get them right.

Here's the code so far:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using Sfs2X.Entities.Data;
 using Sfs2X.Requests;
 
 [RequireComponent (typeof (Inventory))]
 public class TakeItems : Inventory {
     public bool ItemIsFound;
     //public float locOfItem;
     private GameObject item;
 
 
     void Update ()
     {
         if (!ItemIsFound)
         {
             item = GameObject.FindWithTag("Item");
             if(item == null) { return; }
             item.transform.Translate(0, 0, 0);
             GameObject player = GameObject.FindWithTag("Player");
             player.transform.Translate(0, 0, 0);
             ItemIsFound = true;
             Debug.Log("Item Found");
 
         }
     
         if (ItemIsFound)
             if (Input.GetKeyDown(KeyCode.E)) {
                 if (item != null) {
                 
                 ItemStats iss = item.GetComponent<ItemStats> ();
             
                     
                 int adq = 0;
                 for (int i = 0; i < Inventory.inventory.Count; i++)
                 {
 
                     if (Inventory.inventory[i].itemslug == iss.slug && Inventory.inventory[i].itemstackable == true && Inventory.inventory[i].itemquantity < 50)
                     {
                         Inventory.inventory[i].itemquantity++;
                         adq = 1;
                         break;
                     }
                 }
 
                 if (adq == 0)
                 {
                     for (int i = 0; i < Inventory.inventory.Count; i++)
                     {
                         if (Inventory.inventory[i].itemslug == null)
                         {
                             Inventory.inventory[i] = new Item(iss.itemName, iss.id, iss.desc, iss.damage, iss.speed, iss.health, iss.itemType, iss.rarity, iss.slug, iss.stackable, 1);
 
                             break;
 
                         }
                     }
                 }
 
                 ISFSObject objt = SFSObject.NewInstance();
                 objt.PutUtfString("droporpickup", "pickup");
                 objt.PutInt("serveritemid", iss.sid);
                 Debug.Log(iss.sid);
 
                 SFS2X_Connect.sfs.Send(new ExtensionRequest("SpawnMMOItem", objt));
 
 
                 Debug.Log("Took an item");
             }
         }
     }
 }

Comment
Add comment · Show 9
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 gjf · Jan 23, 2016 at 07:55 PM 1
Share

when you get errors, please post the complete error message.

it's probably complaining because item hasn't been set, meaning that the previous line to find it failed. check that your scene has an item tagged appropriately.

it's sometimes beneficial to add some Debug.Log() statements to check that you found what you're looking for... often saves time. you can make it conditional so it won't effect release builds, if you're concerned about performance.

avatar image Lakeyour gjf · Jan 23, 2016 at 08:02 PM 0
Share

You are correct, the scenes don't contain any items when I start up the game.

If I would add an item the error is gone, how would I avoid getting this error in the future?

Also it works to take an item but the removing process seems to not work.

I tend to use debugs :)

avatar image JedBeryll Lakeyour · Jan 23, 2016 at 09:37 PM 0
Share

You avoid it by checking if item is null or not. If its null then don't try to take it.

Show more comments
avatar image JedBeryll · Jan 24, 2016 at 07:23 AM 0
Share

Why not? It's the same:

      if (ItemIsFound)
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) {
              ItemStats iss = item.GetComponent<ItemStats>();
              if (iss != null) {
                  ...
              }
avatar image Lakeyour JedBeryll · Jan 24, 2016 at 12:18 PM 0
Share

Yeah I got it the jist of the null codes now.

However now there's the Collision2D I need some help with, if you would help :)

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Argument out of range? 2 Answers

How to make an Inventory Hotbar (C#) 0 Answers

Help with Interface-free inventory 0 Answers

Why is drag and drop not working? 1 Answer

Canvas Inventory closing 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