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:
Let's say I have a few potions in the world, let's say three for this example
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");
}
}
}
}
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.
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 :)
You avoid it by checking if item is null or not. If its null then don't try to take it.
Why not? It's the same:
if (ItemIsFound)
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E)) {
ItemStats iss = item.GetComponent<ItemStats>();
if (iss != null) {
...
}
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 :)
Your answer
Follow this Question
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